从recyclerview获取选中的复选框列表

时间:2019-04-29 19:12:37

标签: android checkbox

我有一个Recyclerview,其中包含许多复选框。从API收到这些复选框(复选框文本和ID)。我想获取要发送到服务器的已选中复选框的ID列表。我的解决方案之一是:

  1. 创建一个包含方法get和set的数据模型(Info_Checkbox) ID。
  2. 创建ArrayList。
  3. 将选中的复选框ID设置为此数组列表,并使用此数组来 将参数发送到服务器。

但是我的代码不正确!当我选中多个复选框时,数组列表将保存最后一个复选框ID。您能说出另一种解决方案还是解决该错误?

let myReq;

function step(timestamp) {
  myReq = requestAnimationFrame(step); // I understand why we call it here.
  console.log(timestamp, myReq)
  if(timestamp >= 1000) {
    cancelAnimationFrame(myReq);
  }
}

myReq = step;  // why invoke rAF here.

2 个答案:

答案 0 :(得分:0)

您必须在Info_Filter类中创建一个布尔变量,当您选中一个复选框时,将布尔变量值设置为true,而当您取消选中该复选框时,则更改该特定位置的布尔变量值false,之后再您需要获取多少布尔变量为true,多少为false,然后您才能获得准确的项目有多少个检查。

答案 1 :(得分:0)

这应该可以解决问题:

checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = getAdapterPosition();
                    if (pos < 0) {
                        pos = (int) checkBox.getTag();
                    }
                    boolean isChecked = !list.get(pos).isChecked;
                    items.get(pos).isChecked = isChecked;
                    if (isChecked) {
                        selectedCount++;
                        checkBox.setChecked(true);
                    } else {
                        selectedCount--;
                        checkBox.setChecked(false);
                    }
                }
            });

在BindViewHolder中:

setViewHolderFilter.checkBtn.setChecked(list.get(position).isChecked);

现在,您可以在项目ArrayList中获得列表选中的复选框,并且您可以执行任何操作。