如何在android中选择/取消选择ListView数组适配器的RadioButton

Shi*_*ngi -2 android radio-button

我在列表视图适配器中使用单选按钮,我想要的是选择/取消选择我单击的单选按钮。

Pus*_*dra 5

将单选按钮状态保存在列表中,或者如果只有一个选择,在这种情况下,您可以将特定位置保存在 int 变量中,然后在选择后更新变量的值adapter.notifyDataSetChanged()。您需要编写代码来getView反映更改。

粗略的前:(我现在已经习惯了 listview(检查 recyclerview),但你的 adpater 应该看起来像这样)

Adapter{
 int selectedPosition = -1;  // initially nothing selected  

 getView(..,..., int position){

   if(selectedPosition==position){
   holder.radioButton.setChecked(true);
   }else{
   holder.radioButton.setChecked(false);
   }

   holder.radioButton.setTag(position);

   holder.radioButton.onClick(){
   selectedPosition = (Integer)holder.radioButton.getTag();
   notifyDataSetChanged();
   }  

 )


static class Holder {
    RadioButton radioButton;
}
Run Code Online (Sandbox Code Playgroud)