SparseBooleanArray 存储RadioGroup 是否选中第几个

利用SparseBooleanArray 进行存储是否全部选中选项

private SparseBooleanArray mSelectedPositions = new SparseBooleanArray(); //存储是否选中
private List mList = new ArrayList<>();

 @Override
 protected void convert(BaseViewHolder helper,Bean bean) {
     final int position = helper.getLayoutPosition();
     mSelectedPositions.put(position, false);  //每次创建一个View(button)的时候设置默认未选中
     mList.add(position, answer); //赋空值 为了数组不越界

            mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        mSelectedPositions.put(position, true);
                        selected(checkedId);
                  
                        //如果第n个被选中 mList就替换指定位置参数
                        if (mSelectedPositions.get(position)) {
                            mList.set(position, answer);
                        } else {
                            mSelectedPositions.put(position, true);
                            mList.add(position, answer);
                        }
                    }
                });
            }

 /*判断选中第几个赋值ABC...*/
private void selected(int checkedId) {
    switch (checkedId) {
        case 0: {
            selected = "A";
            break;
        }
        case 1: {
            selected = "B";
            break;
        }
        case 2: {
            selected = "C";
            break;
        }
        case 3: {
            selected = "D";
            break;
        }
        case 4: {
            selected = "E";
            break;
        }
        case 5: {
            selected = "F";
            break;
        }
        default: {
            break;
        }
    }
}







 可以在adapter中创建一个方法 这在acticity或fragment中也可以获取到
 public SparseBooleanArray getmSelectedPositions() {
     return mSelectedPositions;
}

 在acticity或fragment中获取
 for (int i = 1; i < mSelectedPositions.size() + 1; i++) {
        if (!mSelectedPositions.get(i - 1)) {
            ToastUtils.showShort(this.getApplicationContext(), "请选中第" + i + "个或者填写其内容");
            break;
        } else {
            if (i == mSelectedPositions.size()) {
                //如果都选中,请求服务器
                if (isSubmit) {
                    getData();
                    isSubmit = false;
                }
            }
        }
    }

你可能感兴趣的:(SparseBooleanArray 存储RadioGroup 是否选中第几个)