I currently develop my own Quiz Application and considering with a problem for 2 days. My Quiz provides 2 variants of questions: multiple choice and single choice. So for the multiple choice questions I'm using CheckBoxes and for the single choice variant there are RadioButtons.
If the user answers a question and goes to the next question, the result will be saved in my SQLite database. If he jumps back to the previous question I'm displaying the saved result.
Here is an example: On Image 1 you see 5 CheckBoxes on the left. These are the correct answers. The right CheckBoxes are the checked answers of the user. So this is the screen you get when you answered the question, clicking on the "next"-button and going back again to your answered question. For the CheckBoxes it works fine and correct.
On Image 2 you see the same use case but this time with a single choice question. And as you can see there is no RadioButton checked on the right side. But in fact I checked the middle answer and the result is also saved in my database.
If I debug my Application it jumps also in the correct section and checks the right RadioButton. But it is never displayed as checked and I can't find the error here.
String resultString = userResultCursor.getString(userResultCursor.getColumnIndexOrThrow(MyDatabase.ResultColumns.RESULT));
String resultArray[] = convertStringToArray(resultString);
List<String> resultList = Arrays.asList(resultArray);
for (String id : resultList) {
if (question.getQuestiontype() == 1) {
for (CheckBox cb : answerCheckBoxes) {
if (cb.getId() == Integer.parseInt(id)) {
cb.setChecked(true);
}
cb.setEnabled(false);
}
}
if (question.getQuestiontype() == 2) {
for (RadioButton rb : answerRadioButtons) {
if (rb.getId() == Integer.parseInt(id)) {
rb.setChecked(true);
}
rb.setEnabled(false);
}
}
What could be the problem?
Here is the layout for the RadioButtons
<LinearLayout
android:id="@+id/layout_singlechoice_answers"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/rb_answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/rb_answer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/rb_answer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/rb_answer5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
EDIT: With Saurabh Padwekar solution it displays the checked RadioButton. But after testing it a bit I noticed this: Sometimes if I want to check a RadioButton it doesn't get checked. Mostly it works but once in a while it doesn't work.
Try clearing the radio group before setting.