radio buttons showing only first option correct and the others showing all wrong data in android

Deadly 提交于 2019-12-24 19:56:19

问题


In my questions answers small game i am showing questions and answers from the database file. question is in text view and answers are in radiobuttons.no the data is showing accurately the way i wanted but when i am selecting the data only the first option of the radio button is selecting as correct ansnwer only if it is the correct answer.my code in mainactivity is as follows `showQuestion = (TextView) findViewById(R.id.tvQuestions); showAnswer = (TextView) findViewById(R.id.tvShowAnswer);

    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
     r0 = (RadioButton) findViewById(R.id.radio0);
    r1 = (RadioButton) findViewById(R.id.radio1);
    r2 = (RadioButton) findViewById(R.id.radio2);
    r3 = (RadioButton) findViewById(R.id.radio3);
    bsubmit = (Button) findViewById(R.id.bsubmit);
    bsubmit.setOnClickListener(this);

              String shoow = myDb.makeatext(levels, Qno);
    String showextra1 = myDb.makeExtra1(levels, Qno);
    String showextra2 = myDb.makeExtra2(levels, Qno);
    String showextra3 = myDb.makeExtra3(levels, Qno);
    String showextra4 = myDb.makeExtra4(levels, Qno);

    // String showextra4 =myDb.makeanswers(levels, Qno);

    showQuestion.setText(shoow);
    r0.setText(showextra1);

    r1.setText(showextra2);

    r2.setText(showextra3);

    r3.setText(showextra4);

and in the onclick() method trying to check the data but it is showing only first radio button answer as correct if the data is correct only.

public void onClick(View v) {
    strat = System.currentTimeMillis();
    data = myDb.makeanswers(levels, Qno);
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bsubmit:
        if (r0.isChecked() == true)

        {
            if (r0.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" +r0.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }
        }
        if (r1.isChecked() == true)

        {
            if (r1.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" + r1.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }

        }
        if (r2.isChecked() == true)

        {
            if (r2.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" + r2.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }

        }
        if (r3.isChecked() == true)

        {
            if (r3.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" + r3.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }
        }}}

plz help me out of it i am new to the android .it will be a great help

thanks, maddy.


回答1:


please try to do like this

case R.id.save_profile:
        if(((RadioButton)findViewById(R.id.radio0)).isChecked()){
            Answer=(RadioButton)findViewById(R.id.radio0)).getText().toString();
        }
       if(((RadioButton)findViewById(R.id.radio1)).isChecked()){
            Answer=(RadioButton)findViewById(R.id.radio0)).getText().toString();
       }  

        // compare here your option......


break;  

after that you need to compare the answer with checked button.




回答2:


your code is perfect. it works for me. Just check if your database retrievals and method makeanswer() are correct.




回答3:


   rb1=(RadioButton)findViewById(R.id.rb1);
   rb2=(RadioButton)findViewById(R.id.rb2);
   rb3=(RadioButton)findViewById(R.id.rb3);
   rb4=(RadioButton)findViewById(R.id.rb4);

rb1.setText("");//get from database options  
rb2.setText("");//get from database options  
rb3.setText("");//get from database options  
rb4.setText("");//get from database options  


//***********onclick of submit button{

int rd1=r_group.getCheckedRadioButtonId();
            String textt = "" ;
/************for error if not click on radio btn**********************/
            if(rd1==-1)

            {
                {   
                AlertDialog ad=new AlertDialog.Builder(Mind_Boost_First.this).create();
                ad.setTitle("error");
                ad.setMessage("plz select any radio button");
                ad.setIcon(R.drawable.cross);
                ad.setButton("ok",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        // TODO Auto-generated method stub

                    }
                });
                ad.show();
                }}  
/****************************** get radio checked to textt**************************/
            else
            {

                switch(rd1)
                {
                    case R.id.rb1:
                textt =rb1.getText().toString().trim();
                        Log.e("rrrrrrrrbbbbbbbb11111111",""+rb1.getText().toString().trim());
                        break;
                    case R.id.rb2:
                textt = rb2.getText().toString().trim();
                        Log.e("rrrrrrrrbbbbbbbb222222222",""+rb1.getText().toString().trim());
                        break;
                    case R.id.rb3:
                textt = rb3.getText().toString();
                        Log.e("rrrrrrrrbbbbbbbb3333333333",""+rb1.getText().toString().trim());
                        break;
                    case R.id.rb4:
                textt = rb4.getText().toString();
                        Log.e("rrrrrrrrbbbbbbbb4444444444",""+rb1.getText().toString().trim());
                        break;
                  }


/*get correct value in str*/

                String str=c.getString(6).trim();

                r_group.clearCheck();




                /*reset value of lable */

This is my snippet of code that works perfect for me



来源:https://stackoverflow.com/questions/8815129/radio-buttons-showing-only-first-option-correct-and-the-others-showing-all-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!