I'm having an issue with RadioGroup's clearChecked(). I'm displaying a multiple choice question to the user and after the user selects an answer I check the answer, give him some feedback and then move to the next question. In the process of moving to the next question I clearCheck on the RadioGroup.
Can anyone explain to me why the onCheckedChanged method is called 3 times? Once when the change actually occurs (with the user changes), once when I clearCheck(with -1 as the selected id) and once in between (with the user changes again)?
As far as I could tell the second trigger is provoked by clearCheck. Code below:
private void checkAnswer(RadioGroup group, int checkedId){
// this makes sure it doesn't blow up when the check is cleared
// also we don't check the answer when there is no answer
if (checkedId == -1) return;
if (group.getCheckedRadioButtonId() == -1) return;
// check if correct answer
if (checkedId == validAnswerId){
score++;
this.giveFeedBack(feedBackType.GOOD);
} else {
this.giveFeedBack(feedBackType.BAD);
}
// allow for user to see feedback and move to next question
h.postDelayed(this, 800);
}
private void changeToQuestion(int questionNumber){
if (questionNumber >= this.questionSet.size()){
// means we are past the question set
// we're going to the score activity
this.goToScoreActivity();
return;
}
//clearing the check
gr.clearCheck();
// give change the feedback back to question
imgFeedback.setImageResource(R.drawable.question_mark); //OTHER CODE HERE
}
and the run method looks like this
public void run() {
questionNumber++;
changeToQuestion(questionNumber);
}
I had similar problem. My solution:
in procedure:
I check checkedId. It equals to -1 if you use clearCheck() else it equals to selected radiogroup child
What I've discovered is that if an item is checked and you call
clearCheck()
on the radio group it will callonCheckedChanged
twice. The first time with the id of the item that was checked and the second time with-1
/View.NO_ID
. IMHO, this is a bug and apparently it has been around since at least 1.6. See this google code bug report: http://code.google.com/p/android/issues/detail?id=4785It seems to be that the only solution is to check the actual
RadioButton.isChecked()
and test if it is true or false. This sort of defeats the purpose of theonCheckedChanged
returning the id of the item since you now have to either keep references to those buttons or callfindViewById
every time.I doubt they will fix this since changing it would probably break existing code in unexpected ways.
I faced the same problem and i solved with other work-around.
Code snnipet :
Hope this will help ツ