0

I am developing a small quiz app. The correct answers are stored in a string-array, so when the user select one of the options (radioButton for each option), I make a string comparisson.

If the answer was wrong, there should be a iteration on the other radioButtons to verify which has the same string as my answer array, to mark that was the right answer.

correctAnswer = answers[position];
int currentAnswerId = optionsRadioGroup.getCheckedRadioButtonId();
    RadioButton selectedAnswer = (RadioButton) findViewById(currentAnswerId);
    if(selectedAnswer.getText().equals(correctAnswer)) {
                    selectedAnswer.setTextColor(Color.GREEN);
                }
                else {
                    selectedAnswer.setTextColor(Color.RED);
                    //search the other radioButtons for the matching string

                }

Any help would be appreciated, guess this one is not hard but I'm new to Android and Java Dev.

artdias90
  • 735
  • 1
  • 10
  • 18
  • Thanks Mohamed, I tried to search before but didn't find this one. – artdias90 Jan 05 '14 at 09:34
  • Wouldn't it be better if your radiobuttons set an int and you compared it with an answer index? **Text comparison is more expensive than integer comparison**. – Phantômaxx Jan 05 '14 at 09:59

1 Answers1

5

You can simply loop through all the child views of the radio group and

int count = radioGroup.getChildCount();
        for (int i=0;i<count;i++) {
            View o = radioGroup.getChildAt(i);
            if (o instanceof RadioButton) {
                RadioButton selectedAnswer =(RadioButton)o;
                if(selectedAnswer.getText().equals(correctAnswer)) {
                    selectedAnswer.setTextColor(Color.GREEN);
                }
            }
        }
vipul mittal
  • 17,343
  • 3
  • 41
  • 44