-1

I have created programmatically 4 radio groups with 4 radio buttons. Each radio button represents an answer to a question. Let's say that we have in the first radio group, 3 wrong answers and only one correct answer. The first, the second and the third are wrong and the fourth is correct. When someone checks only one radio button, let say for example the first one, which is a wrong one, this goes to the wrongAnswersRadios array list. But if someone checks the first one and then is changing his mind and checks the last one (the correct one) than the first one that was checked goes to wrongAnswersRadios array list and the last one that was checked, goes to correctAnswerRadios array list. I think onClick fires every time a radio button is checked.
How do i manage to add in the array lists only the last checked radio button from each radio group? Here is my code:

correctAnswerRadios = new ArrayList<>();
wrongAnswersRadios = new ArrayList<>();

radioGroup.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        for (int j = 0; j < 4; j++) {
            checkedRadioButton[j] = ((RadioButton) v);
            if (checkedRadioButton[j].isChecked() & CorrectAnswer == 1) {
                correctAnswerRadios.add(checkedRadioButton[j]);
            } else {
                wrongAnswersRadios.add(checkedRadioButton[j]);
            }
        }
    }
});
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • why not create an additional button and set onclicklistener to it and then get the last selected id using getCheckedRadioButtonId(). – Maverick Nov 12 '15 at 08:37
  • Why to do that? I don't need an additional button. All i need is to have only the last checked radio button in the array list that belongs. – Alex Mamo Nov 12 '15 at 09:05

1 Answers1

0

You can do something like this:

correctAnswerRadios = new ArrayList<>();
wrongAnswersRadios = new ArrayList<>();

radioGroup.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        for (int j = 0; j < checkedRadioButton.length; j++) {            
            correctAnswerRadios.remove(checkedRadioButton[j]);
            wrongAnswersRadios.remove(checkedRadioButton[j]);
        }
        for (int j = 0; j < checkedRadioButton.length; j++) {
            if (checkedRadioButton[j].isChecked() & CorrectAnswer == 1) {
                correctAnswerRadios.add(checkedRadioButton[j]);
            } else {
                wrongAnswersRadios.add(checkedRadioButton[j]);
            }
        }
    }
});
Titus
  • 22,031
  • 1
  • 23
  • 33
  • If i do like this, in the end both list are empty. – Alex Mamo Nov 12 '15 at 09:03
  • @AlexM. They shouldn't be because the second loop should add the radio button to the appropriate list (maybe the `CorrectAnswer == 1` condition isn't met). – Titus Nov 12 '15 at 09:05
  • @AlexM. Make sure the loops are set for your context, I've changed `j < 4` to `j < checkedRadioButton.length`. – Titus Nov 12 '15 at 09:07
  • Yes it's correct `checkedRadioButton.length` has the length of 4, but doesn't work. – Alex Mamo Nov 12 '15 at 09:18
  • @AlexM. I see the problem now, you're setting all the elements in the array to the clicked radio button `checkedRadioButton[j] = ((RadioButton) v);`. I'm not sure why you're doing that. – Titus Nov 12 '15 at 09:23
  • Ok, but how do i change that to have what i need? – Alex Mamo Nov 12 '15 at 09:33
  • @AlexM. I'm not sure because I don't know exactly what you need. I've edited my answer, see if it will works now. – Titus Nov 12 '15 at 09:36
  • If i remove `checkedRadioButton[j] = ((RadioButton) v);`, i get this error: `Attempt to invoke virtual method 'java.lang.Object android.widget.RadioButton.getTag()' on a null object reference`. This is happening because i also have `int CorrectAnswer = Integer.parseInt(checkedRadioButton[j].getTag().toString());` that is in the if statement. – Alex Mamo Nov 12 '15 at 09:42
  • @AlexM. I can't solve this without some context. The error says that the `checkedRadioButton` array has `null` elements in it. – Titus Nov 12 '15 at 09:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94910/discussion-between-titus-and-alex-m). – Titus Nov 12 '15 at 10:11