listView嵌套radiobutton

来源:互联网 发布:spss如何输入数据 编辑:程序博客网 时间:2024/05/11 01:29

跟大多数一样,思路就是将状态存储到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