Search code examples
javaandroidif-statementfor-loopchangelistener

How to display different messages when different radio buttons are checked in Java?


I want to display "You clicked the wrong answer", when the first radio button is checked and "You clicked the correct answer", when the second radio button is checked. With this code, i get an error: Cannot resolve symbol 'CorrectAnswer'. The bariable CorrectAnswer is the result of a database query. Here is my code:

    radioGroup = new RadioGroup[2];
    answer = new RadioButton[2];
    int i = 0;
    for (Question qn : questions) {
        radioGroup[i] = new RadioGroup(this);
        int j = 0;
        for (Answer an : answers) {
            if (qn.getID() == an.getQuestion_id_answer()) {
                String answers_log = " " + an.getAnswer();
                Integer CorrectAnswer = an.getCorrect_answer();
                answer[j] = new RadioButton(this);
                answer[j].setText(answers_log);
                radioGroup[i].addView(answer[j]);
                j++;
            }
        }
        linearLayout.addView(radioGroup[i]);

        radioGroup[i].setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case 0:
                        if (CorrectAnswer == 1) {
                            Toast.makeText(getApplicationContext(), "You clicked the correct answer ", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "You clicked the incorrect answer ", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 1:
                        if (CorrectAnswer == 1) {
                            Toast.makeText(getApplicationContext(), "You clicked the correct answer ", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "You clicked the incorrect answer ", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        });
        i++;
    }

The database structure is like this:

         db.addAnswer(new Answer("NY", 3, 0));
         db.addAnswer(new Answer("WA", 3, 1));

As you see, in the third column i have 1, this means that the second answer it's true.
Thanks!


Solution

  • i get an error: Cannot resolve symbol 'CorrectAnswer'

    Because CorrectAnswer variable is not in scope of method where trying to access it.

    Either declare it as global variable or use setTag/getTag of RadioButton to get CorrectAnswer value in onCheckedChanged

    I Think use setTag/getTag methods for setting CorrectAnswer as:

    1. First save value with RadioButton Using setTag:

    answer[j] = new RadioButton(this);
      answer[j].setTag(String.valueOf(an.getCorrect_answer()));
      answer[j].setText(answers_log);
    

    2. In onCheckedChanged get selected RadioButton value as:

     @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
        int  CorrectAnswer=Integer.parseInt(checkedRadioButton.getTag().toString());
        ....your code here...
      }