android 单选按钮 全部不选中,android – 单选按钮setChecked / setSelected不起作用

我有一个带有文本和3个单选按钮的列表视图.如果我选择任何单选按钮,并滚动列表,单选按钮将被取消选中.我正在维护选择了单选按钮的项目的ID,并在适配器Getview设置中选择所需的单选按钮.在调试时,它正在执行语句setChecked(true)但单选按钮仍未选中.我尝试了setSelected(true)并通过radio组设置((RadioButton)holder.rg.getChildAt(2)).setChecked(true);但似乎没有任何工作.

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"/>

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">

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"/>

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"/>

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"/>

适配器代码:

private class ViewHolder {

TextView productName;

RadioButton regularBtn, smallBtn, extraSmallBtn;

RadioGroup rg;

}

适配器中的GetView方法

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);

}

}

}

}

我不确定我是否遗漏了什么,但是setChecked或setSelected不起作用.请指教.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值