9

I have a list view with a text and 3 radio button. If i select any radio button, and scroll the list the radio button gets un-selected. I am maintaining the ID of items for which radio button is selected and in adapter Getview setting the required radio button to be selected. On debugging, it's executing statement setChecked(true) but the radio button is still unchecked. I tried setSelected(true) and setting through radio group ((RadioButton)holder.rg.getChildAt(2)).setChecked(true); but nothing seem to be working.

XML:

<TextView
        android:id="@+id/refill_product_name"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="@string/hello_world"
        android:textColor="@android:color/black"
        android:textStyle="normal"
        android:textSize="@dimen/item_sub_heading"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"/>

    <RadioGroup
        android:id="@+id/refill_product_rg"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/refill_product_regular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/regular"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:buttonTint="@color/primary"/>

        <RadioButton
            android:id="@+id/refill_product_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/small"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:layout_marginLeft="@dimen/radio_btn_margin"
            android:buttonTint="@color/primary"/>

        <RadioButton
            android:id="@+id/refill_product_extra_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/extra_small"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:layout_marginLeft="@dimen/radio_btn_margin"
            android:buttonTint="@color/primary"/>

    </RadioGroup>

Adapter Code:

private class ViewHolder {
        TextView productName;
        RadioButton regularBtn, smallBtn, extraSmallBtn;
        RadioGroup rg;
    }

GetView Method in Adapter

holder.rg.clearCheck();
if (mAppSession.getSelRefillProducts() != null) {
    for (RefillProduct pr : mAppSession.getSelRefillProducts()) {
         if (pr.getProductId() == rf.getProductId()) {
            if (pr.getSize().equals("R")) {
               ((RadioButton)holder.rg.getChildAt(0)).setChecked(true);
             } else if (pr.getSize().equals("S")) {
                holder.smallBtn.setSelected(true);
             } else if (pr.getSize().equals("XS")) {
               ((RadioButton)holder.rg.getChildAt(2)).setChecked(true);
             }
          }
         }
      }

I am not sure if I am missing something, but setChecked or setSelected is not working. Please advise.

Gaurav
  • 535
  • 1
  • 8
  • 28
  • 3
    `setSelected` is something **else** completely. Don't even expect that to work. Now let me take a look and try to deduce why `setChecked` does not work. Try adding `android:clickable="true" ` to radio buttons, see if it makes a difference. – Vucko Jul 08 '16 at 10:11
  • Tried as suggested. But as expected, no difference. Actually, it doesn't seem to be recycling view issue as the selected radio button gets unchecked and none of the list item has the radio button checked. – Gaurav Jul 08 '16 at 10:17
  • 1
    You cannot loop through every item's Radio Button in a Single method Using single `holder` object. **`(RadioButton)holder.rg.getChildAt(0)).setChecked(true);` will check only ONE Radio Button not Every Radio Button in the whole list.** – Janki Gadhiya Jul 08 '16 at 10:25
  • I believe GetView method in adapter is called for each item in the list and and so we can set the state of child items for each item in list view. During debugging it's executing statement setChecked(true) but still the radio button in unchecked. – Gaurav Jul 08 '16 at 10:28
  • @JankiGadhiya 's comment worked for me! – MMK Feb 15 '20 at 19:38

3 Answers3

53

Try this, before setting radio button to checked, clear all the checks in the radio group:

yourRadioGroup.clearCheck();
yourRadioButton.setChecked(true);
Ashwin
  • 7,277
  • 1
  • 48
  • 70
0

I solved this by removing if (convertView == null) and the else part from getView method in adapter.

else {
 holder = (ViewHolder) convertView.getTag();
Gaurav
  • 535
  • 1
  • 8
  • 28
-1

It is an expected behavior of ListView since it reuses the View for each item. Consider tracking the checkbox checked state using a SparseBoolArray. Check out this link: Getting the state of a checkbox inside of a listview

Srijith
  • 1,695
  • 17
  • 29
  • Thanks for the answer. I understand this behavior and I have maintained the list with item ID & corresponding option. In my GetView method, I am checking each item with my list and then setting the radio button as checked or not. On debugging, its executing the statement setChecked(true) but still not working. – Gaurav Jul 08 '16 at 10:25