listView嵌套radiobutton

跟大多数一样,思路就是将状态存储到map中,再回显

定义map存储选中的状态,key为item的position,值为0和1代表这选择的是第一个radiobutton还是第二个,这里是二个radiobutton,其他的类似

private Map<Integer, Integer> mapRadioBtn=new HashMap<Integer, Integer>();

别的不贴了。贴一下getview方法,主要就是getview

public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView==null) {
            convertView=View.inflate(getApplicationContext(), R.layout.item_activity_addbuildingrecode, null);
            }
            final RadioButton radioBtn1=ViewHolder.get(convertView, R.id.item_addbuildingrecode_radioBtn1);
            final RadioButton radioBtn2=ViewHolder.get(convertView, R.id.item_addbuildingrecode_radioBtn2);
             final RadioGroup mRadioGroup=ViewHolder.get(convertView, R.id.item_addbuildingrecode_radioGroup);
            //          mRadioGroup.setTag(position);
            mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) 
                {

                    if (radioBtn1.isChecked()||radioBtn2.isChecked()) {
                        if (checkedId==radioBtn1.getId()) 
                        {//设定radiobutton1为0,radiobutton2为1
                            mapRadioBtn.put(position, 0);
                            MLog.e("main", "put"+position);
                        }else if (checkedId==radioBtn2.getId()) 
                        {
                            mapRadioBtn.put(position, 1);
                            MLog.e("main", "put"+position);
                        }
                    }else {

                    }
                }
            });

            if (mapRadioBtn.keySet()!=null&&mapRadioBtn.containsKey(position)) {
                MLog.e("main","恢复keyset"+mapRadioBtn.keySet().toString() +position);
                if (mapRadioBtn.get(position)==0) {
                    radioBtn2.setChecked(true);//恢复第一个选中(先选中第二个,实现切换效果,否则重复选择第一个,会出现第一个以选择但是选中图标不出现)
                    MLog.e("main","选中第一个");
                    radioBtn1.setChecked(true);
                }else if (mapRadioBtn.get(position)==1) {
                    radioBtn1.setChecked(true);
                    MLog.e("main","选中第二个");
                    radioBtn2.setChecked(true);
                }

            }else {
                MLog.e("main","不包含keyset"+mapRadioBtn.keySet().toString()+position );
                radioBtn1.setChecked(false);
                radioBtn2.setChecked(false);
            }
            if (mapRadioBtn!=null) {
                MLog.e("main","map的keyset"+mapRadioBtn.keySet().toString() );
            }
            return convertView;
        }

其中在恢复时发现了个bug,调了很久,本来走了恢复的方法,但是一直不是选中状态,经研究发现,确实已经选中了,只是界面选中效果看不到,即图标没有换为选中图标,不知为何,自己的解决办法有点low,就是先选择一下另一个在选回来,这样效果救出来了(研究时发现如果第一个被选中,在恢复时有setchecked一下它,就会变为为选中状态图标,所以想到切换一下,避免重复选中一个radiobutton,low死了,但是解决了),如果有帮助可以借鉴一下,有好的办法也分享一下哈,谢谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值