Ramya Yadav
Ramya Yadav

Reputation: 29

How to hold selected value of radio button?

I am doing quiz app. I have set of 5 questions and each question contains 4 options(radio group) with previous and next button. What I need is when user select the answer and goes for next question and again come back to previous question,what user selected previously that answer should be in selected state.. Please any can help me I am new to android..

public class MainActivity extends AppCompatActivity {
         List<Questions> quesList;
         int score=0;
         int qid;
        Questions currentQ;
        TextView tv;
        RadioButton rb1,rb2,rb3,rb4;
        ImageButton next,back;
        RadioGroup grp;
        Questions cur;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            DbHelper db=new DbHelper(this);
             grp=(RadioGroup) findViewById(R.id.radiogroup1);
            quesList=db.getAllQuestions();
            if(quesList!= null && quesList.size() !=0) {
                currentQ=quesList.get(qid);
            }

            tv=(TextView) findViewById(R.id.tv1);
            rb1=(RadioButton) findViewById(R.id.radio1);
            rb2=(RadioButton) findViewById(R.id.radio2);
            rb3=(RadioButton) findViewById(R.id.radio3);
            rb4=(RadioButton) findViewById(R.id.radio4);

            next=(ImageButton) findViewById(R.id.forward);
            back=(ImageButton) findViewById(R.id.backward);
            setQuestionView();
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    RadioButton answer=(RadioButton) findViewById(grp.getCheckedRadioButtonId());
                    if(currentQ.getAnswer().equals(answer.getText()))
                   {
                        score++;
                        Log.d("score", "Your score" + score);
                    }

                    if(qid<4){

                        qid++;
                      currentQ=quesList.get(qid);

                        grp.clearCheck();
                        setQuestionView();



                    }else{
                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        Bundle b = new Bundle();
                        b.putInt("score", score); //Your score
                        intent.putExtras(b); //Put your score to your next Intent
                        startActivity(intent);
                        finish();
                    }

                }
            });

            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(qid>0){
                        qid--;
                        currentQ=quesList.get(qid);
                        setQuestionView();


                    }
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
        private void setQuestionView()
        {
            tv.setText(currentQ.getQuestion());
            rb1.setText(currentQ.getOption1());
            rb2.setText(currentQ.getOption2());
            rb3.setText(currentQ.getOption3());
            rb4.setText(currentQ.getOption4());


        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }

Upvotes: 0

Views: 1251

Answers (3)

Minato
Minato

Reputation: 4533

You can define an array or a list to store the answers and initially assign 0s and when user selects answer for any given question answers[qid] = //answer 1,2,3,4 and lastly modify your setQuestionView() to

private void setQuestionView()
    {
        tv.setText(currentQ.getQuestion());
        rb1.setText(currentQ.getOption1());
        rb2.setText(currentQ.getOption2());
        rb3.setText(currentQ.getOption3());
        rb4.setText(currentQ.getOption4());
        switch(answers[qid]){
            case 1:
              rb1.setChecked(true);
              break;
            case 2:
              rb2.setChecked(true);
              break;
            ...........
            default:
              break;
        }

    }

P.S I would not recommend the above method instead I would suggest re-implementation/factoring of your code to involve this behavior.

Upvotes: 2

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

Try something like this

private void save(int radioid,final boolean isChecked) {

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check"+radioid, isChecked);
editor.commit();
}

and to get values

private void load() { 
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
for(int i=0;i<radioGroup.getChildCount();i++){
   RadioButton rbtn=(RadioButton)radioGroup.getChildAt(i);
   rbtn.setChecked(sharedPreferences.getBoolean("check"+rbtn.getId(), false)); 
 }
}

Upvotes: 1

corda dev
corda dev

Reputation: 169

When you will give the answer then get the radio button id and save into Arraylist, in which list you are currently used for show the question.

For example : singleton.globalList.get(Constants.GlobalPostion).setAnswer_type(""+buttonID);

And put the if condition in oncreate method,check if you have given the question if yes then get the value from list and set that id true.

For ex:

    if (isSolve())
    {
          buttonID=**get id which you have been saved in arraylist**.........getAnswer_type());
         RadioButton btn = (RadioButton) rg.getChildAt(buttonID);
         btn.setChecked(true);
    }

hope this will help you.

Upvotes: 0

Related Questions