こんにちは、複数のアクティビティが発生する Java で複数のシークバー レイアウトを呼び出そうとしています。しかし、私はそれを行う方法がわかりません。これが私のサンプルJavaコードです
US1 = (SeekBar) findViewById(R.id.US1);
US2 = (SeekBar) findViewById(R.id.US2);
US3 = (SeekBar) findViewById(R.id.US3);
if(day=1)
{
if (Q=1)
{
US1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q1U1 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d1Q1U1.setText(Integer.toString(progress)+"%") ;
}
US2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q1U2 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d1Q1U2.setText(Integer.toString(progress)+"%") ;
}
US3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q1U3 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d1Q1U3.setText(Integer.toString(progress)+"%") ;
}
//later on I need to use d1q1U1,d1q1U2,d1q1U3 for storing the variables
}
else if(Q=2)
{
US1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q2U1 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d1Q2U1.setText(Integer.toString(progress)+"%") ;
}
US2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q2U2 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d1Q2U2.setText(Integer.toString(progress)+"%") ;
}
US3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q2U3 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d1Q2U3.setText(Integer.toString(progress)+"%") ;
}
}
//later on I need to use d1q2U1,d1q2U2,d1q2U3 for storing the variables
}
}
else id(day=2)
{
if (Q=1)
{
US1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d2Q1U1 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d2Q1U1.setText(Integer.toString(progress)+"%") ;
}
US2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q1U2 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d2Q1U2.setText(Integer.toString(progress)+"%") ;
}
US3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d2Q1U3 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d2Q1U3.setText(Integer.toString(progress)+"%") ;
}
//later on I need to use d2q1U1,d2q1U2,d2q1U3 for storing the variables
}
else if(Q=2)
{
US1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d2Q2U1 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d2Q2U1.setText(Integer.toString(progress)+"%") ;
}
US2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d1Q2U2 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d2Q2U2.setText(Integer.toString(progress)+"%") ;
}
US3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
TextView d2Q2U3 = (TextView)findViewById(R.id.UPer1);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
d2Q2U3.setText(Integer.toString(progress)+"%") ;
}
}
//later on I need to use d2q2U1,d2q2U2,d2q2U3 for storing the variables
}
}
これは私のコードがどのように見えるかです私が試みていた単一のJavaクラスでこれを行うにはどうすればよいですか
これは、d=1 と Q=1 を選択したと仮定したときの私の実際のコードです。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.day1q1);
myValues=getIntent().getExtras();
US1 = (SeekBar) findViewById(R.id.US1);
US1.setOnSeekBarChangeListener(this);
US2= (SeekBar) findViewById(R.id.US2);
US2.setOnSeekBarChangeListener(this);
US3= (SeekBar) findViewById(R.id.US3);
US3.setOnSeekBarChangeListener(this);
US4= (SeekBar) findViewById(R.id.US4);
US4.setOnSeekBarChangeListener(this);
US5= (SeekBar) findViewById(R.id.US5);
US5.setOnSeekBarChangeListener(this);
US6= (SeekBar) findViewById(R.id.US6);
US6.setOnSeekBarChangeListener(this);
UPer1= (TextView)findViewById(R.id.UPer1);
UPer2= (TextView)findViewById(R.id.UPer2);
UPer3= (TextView)findViewById(R.id.UPer3);
UPer4= (TextView)findViewById(R.id.Uper4);
UPer5= (TextView)findViewById(R.id.UPer5);
UPer6= (TextView)findViewById(R.id.UPer6);
TextView welcomeMsg = (TextView)findViewById(R.id.username);
welcomeMsg.setText("name : "+myValues.getString("value"));
User1 = (TextView) findViewById(R.id.UN1);
User2 = (TextView) findViewById(R.id.UN2);
User3 = (TextView) findViewById(R.id.UN3);
User4 = (TextView) findViewById(R.id.UN4);
User5 = (TextView) findViewById(R.id.UN5);
User6 = (TextView) findViewById(R.id.UN6);
// Getting complete product details in background thread
new Userdetails().execute();
ok = (Button)findViewById(R.id.ok);
nextQ = (Button)findViewById(R.id.nextquestion);
cancel= (Button)findViewById(R.id.cancel);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(day1q1.this, "",
"storing the data...", true);
new Thread(new Runnable() {
public void run() {
insert();
}
}).start();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myIntent=new Intent(day1q1.this, day1q1.class);
String uname;
uname=myValues.getString("value").toString();
myIntent.putExtra("value", uname);
startActivity(myIntent);
}
});
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId())
{
case R.id.US1:
UPer1.setText(Integer.toString(progress)+"%") ;
//Toast.makeText(discipline.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
break;
case R.id.US2:
UPer2.setText(Integer.toString(progress)+"%") ;
//Toast.makeText(discipline.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
break;
case R.id.US3:
UPer3.setText(Integer.toString(progress)+"%") ;
//Toast.makeText(discipline.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
break;
case R.id.US4:
UPer4.setText(Integer.toString(progress)+"%") ;
//Toast.makeText(discipline.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
break;
case R.id.US5:
UPer5.setText(Integer.toString(progress)+"%") ;
//Toast.makeText(discipline.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
break;
case R.id.US6:
UPer6.setText(Integer.toString(progress)+"%") ;
//Toast.makeText(discipline.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
void insert(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://5.39.186.164/day1q1.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>();
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("user",myValues.getString("value").toString()));
nameValuePairs.add(new BasicNameValuePair("UPer1",UPer1.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("UPer2",UPer2.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("UPer3",UPer3.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("UPer4",UPer4.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("UPer5",UPer5.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("UPer6",UPer6.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Un1",User1.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Un2",User2.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Un3",User3.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Un4",User4.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Un5",User5.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Un6",User6.getText().toString()));
//System.out.println(myValues.getString("value"));
//System.out.println(Q9a.getText());
// $Edittext_value = $_POST['Edittext_value'];
//nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
//response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
//Entered data successfully\r\n\r\n\r\n
if(response.equalsIgnoreCase("Entered data successfully\n\n\n")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(day1q1.this,"Inserted Successully", Toast.LENGTH_SHORT).show();
}
});
myIntent=new Intent(day1q1.this, menu.class);
String uname;
uname=myValues.getString("value").toString();
myIntent.putExtra("value", uname);
startActivity(myIntent);
}else{
showAlert();
startActivity(new Intent(day1q1.this, day1q1.class));
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(day1q1.this);
builder.setTitle("error in inserting.");
builder.setMessage("not inserted.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
しかし今、代わりに値を割り当てるために動的にしたい Uper1、Uper2and .. d = 1、Q = 1を選択したときのように、名前をd1q1u1、d1q1u2、...として選択し、d = 1、Q =を選択したとき2 は d1q2u1、d1q2u2... と命名し、d=2、Q=1 を選択した場合は d2q1u1、d2q1u2 と命名し、d=2、Q=2 を選択した場合は同じ構造に従う必要があります。 q 前の画面選択から。
それで、これを解決する方法を教えてください。