Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button setChecked/setSelected not working

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.

like image 281
Gaurav Avatar asked Jul 08 '16 10:07

Gaurav


1 Answers

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

yourRadioGroup.clearCheck();
yourRadioButton.setChecked(true);
like image 136
Ashwin Avatar answered Oct 07 '22 17:10

Ashwin