0

嗨,我正在尝试在发生多个活动的 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();               
                }
            });

        }

But now I want make it dynamic for assigning the values instead Uper1,Uper2and .. as when selected d=1,Q=1 then I want the names as d1q1u1,d1q1u2,.... and when selected d=1,Q= 2然后名称为d1q2u1,d1q2u2 ...和选择d = 2,q = 1时,然后名称为d2q1u1,d2q1u2 ...,并且在选择d = 2时,需要对相同的结构进行填充q 来自先前的屏幕选择。

所以请告诉如何解决这个问题。

4

0 回答 0