Android开发之LitView中嵌入RadioButton,点击事件混乱现象解决

Android开发之LitView中嵌入RadioButton,点击事件混乱现象解决

1、问题描述

在listview_detetion.xml文件中放置了两个RadioButton,代码如下所示:

<RadioGroup
      android:id="@+id/rb_detection_group"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:orientation="horizontal">

      <RadioButton
        android:id="@+id/rb_detection_1_1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
          android:background="@color/floralwhite"
        android:text="合格"
        android:textSize="15sp" />

      <RadioButton
        android:id="@+id/rb_detection_2_2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
          android:background="@color/floralwhite"
        android:text="不合格"
        android:textSize="15sp" />
    </RadioGroup>

利用Activity中listview的适配器让其在ListView中的每一项都可以显示,但是在实际测试中发现,当listview有多条记录时,点击前面的RadioButton时,下面的某一项也会随着上面的点击事件,显示一样的选择项。

2、解决方案

经过查看资料,发现了一篇文章,测试后发现解决了之前的问题。文章地址:地址
自己Adapter和ViewHolder的代码如下:

class DetectionAdapter1 extends BaseAdapter{

        private Map<Integer,Integer> hashMap = new HashMap<>();// key封装的是它爹的tag值,value封装儿子radiobutton

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            //如果没有复用的
            if(convertView == null){
                //加载item的布局
                convertView = View.inflate(DetctionActivity.this, R.layout.listview_detection1, null);
                viewHolder = new ViewHolder();
                viewHolder.contentView= (TextView) convertView.findViewById(R.id.tv_detection_item1);
                viewHolder.rb_detection_1 = convertView.findViewById(R.id.rb_detection_1_1);
                viewHolder.rb_detection_2 = convertView.findViewById(R.id.rb_detection_2_2);
                viewHolder.rg_detection_group = convertView.findViewById(R.id.rb_detection_group);
                convertView.setTag(viewHolder);
            }
            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.rg_detection_group.setTag(position);//给RadioGroup  弄个tag标记
            if(hashMap.containsKey(position))
            {
                viewHolder.rg_detection_group.check(hashMap.get(position));
            }
            else
            {
                viewHolder.rg_detection_group.clearCheck();
            }

            viewHolder.rg_detection_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if ((Integer)group.getTag() == position){
                        boolean b = false;
                        if (checkedId  == R.id.rb_detection_1_1){
                            b = true;
                            hashMap.put((Integer) group.getTag(),R.id.rb_detection_1_1);
                        }else if (checkedId == R.id.rb_detection_2_2){
                            b = true;
                            hashMap.put((Integer) group.getTag(),R.id.rb_detection_2_2);
                        }

                    }
                }
            });

            viewHolder.contentView.setText(listData.get(position).getCHECKCONTENT());
            viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(this, "click "+((TextView)v).getText(), Toast.LENGTH_SHORT).show();
              
                }


            });
            viewHolder.rb_detection_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                        listData.get(position).setResultStatus("合格");
                }
            });


            viewHolder.rb_detection_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    listData.get(position).setResultStatus("不合格");
                }
            });

            return convertView;
        }
    }
     
    }

    static class ViewHolder
    {
        public TextView contentView;
        public TextView menuView;
        public RadioButton rb_detection_1;
        public RadioButton rb_detection_2;
        public RadioGroup rg_detection_group;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值