Radio Button setChecked/setSelected not working
Asked Answered
C

3

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.

Claqueur answered 8/7, 2016 at 10:8 Comment(5)
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.Fagen
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.Claqueur
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.Haight
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.Claqueur
@JankiGadhiya 's comment worked for me!Whited
J
54

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

yourRadioGroup.clearCheck();
yourRadioButton.setChecked(true);
Joker answered 4/5, 2017 at 15:19 Comment(2)
I don't know why it this to be done like this but this answered saved me after 3 hours of debuggingVictim
Absolute life saver!Wendelina
C
0

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

else {
 holder = (ViewHolder) convertView.getTag();
Claqueur answered 8/7, 2016 at 11:4 Comment(0)
K
-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

Kyl answered 8/7, 2016 at 10:20 Comment(1)
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.Claqueur

© 2022 - 2024 — McMap. All rights reserved.