2

I have a ListView with several items so that these will be re-used during scrolling. An item is composed by a couple of TextViews and a RadioGroup, containing 3 RadioButtons.

The issue I'm having is that when I select for example a RadioButton for each of the 5 items, when I scroll the list down and back up again, the selection I did in the beginning is not being saved (which means no RadioButton or the wrong one will be shown as selected) while it can also happen that some RadioButtons will appear as selected even if I never tapped on those.

I'm trying to save the checkedId of the selection in a list but without luck.

Following my code:

public View getView(int position, View convertView, ViewGroup parent) {
    ...
    int questionID = question.getQuestionID();
    if (mappedSelectedAnswer.containsKey(questionID)) {
        radioGroup.check(mappedSelectedAnswer.get(questionID));
    } else {
        radioGroup.check(-1);
    }
    ...
}

And the RadioGroupListener

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {
            mappedSelectedAnswer.put(questionID, checkedId);
        }
    ...
    });

mappedSelectedAnswer is a List with key questionID, value the checkedId

As anyone have any suggestion to this issue?

Thanks

ace_ventura
  • 468
  • 1
  • 4
  • 14
  • This is probably due to your `RadioGroup` being recycled as you scroll. [Check out this answer.](http://stackoverflow.com/questions/2937581/android-how-to-make-radiogroup-work-correctly-in-a-listview) – Evan Bashir Oct 31 '16 at 00:18
  • As I already specified I already know what's the cause, what I need is an efficient solution. I checked the answer you linked but i don't get the point. Can you be more specific? thanks – ace_ventura Oct 31 '16 at 01:28

3 Answers3

0

you might need to declare all of them into separate RadioGroup to select them for each Item. I hope it will help you.

Thanks

Rahul
  • 130
  • 2
0

You need three things

1- A List to store the selected ID of each RadioGroup

2- A listener in the RadioGroup to set/reset the selected id when u tab on it

3- The adapter will check this list when creating the view and select the item that you saved in the listener

Ahmed Ghonim
  • 4,455
  • 1
  • 17
  • 23
  • I did that, the problem is that when I scroll and I check the radio button, also the `onCheckedListener` will be called and will set the current value for also another item. – ace_ventura Oct 31 '16 at 01:26
  • The listener looks fine. Where do you get the question and the radioGroup from? – Ahmed Ghonim Oct 31 '16 at 01:51
  • Question comes from `getItem(position)`, Radiogroup comes from `convertView.findViewById` – ace_ventura Oct 31 '16 at 02:29
0

For Example:

<RadioGroup
    android:id="@+id/group"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Standard Taxi (upto 4 passengers)"
        android:id="@+id/standardTaxi"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:onClick="radio"
        android:checked="true" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Wagon(upto 4 passengers)"
        android:id="@+id/wagon"
        android:layout_below="@+id/standardTaxi"
        android:layout_alignParentStart="true"

        android:onClick="radio"
        android:checked="false" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Wheelchair Accessible Taxi"
        android:id="@+id/wheelchair"
        android:layout_below="@+id/wagon"
        android:layout_alignParentStart="true"
        android:onClick="radio"

        android:checked="false" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Van/Maxi(5-11 passengers)"
        android:id="@+id/van"
        android:layout_centerVertical="true"
        android:onClick="radio"
        android:layout_alignParentStart="true"
        android:checked="false" />
</RadioGroup>
Rahul
  • 130
  • 2