【问题标题】:How to sum checked items data values in RecyclerView如何在 RecyclerView 中汇总检查项目数据值
【发布时间】:2019-12-27 17:57:02
【问题描述】:

我的问题是如何汇总所有选中项的值,以及如何在选中/取消选中事件中管理它们的更改? 这是我的复选框切换(选中/取消选中)事件的方法。

holder.itemCheckBox.setOnClickListener(new View.OnClickListener() 
{
  @Override
  public void onClick(View v) 
  {
    if (((CheckBox) v).isChecked()) 
    {
      double price = Double.parseDouble(catalogDatabases.get(position).getPriceItem());
      holder.totalPrice = holder.totalPrice + price;
      holder.listener.respond(holder.totalPrice);
    } 
    else {
    }
  }
});

【问题讨论】:

  • 你说的是列表适配器吗?您的目的是保存所有检查项目的总和?
  • @RamiLoiferman 是的
  • holder.totalPrice的类型是什么?
  • @RamiLoiferman 'double totalPrice = 0;'
  • @afiq,你能弄清楚你这样做时面临的问题是什么吗?

标签: java android android-studio


【解决方案1】:

所以首先不要依赖视图作为数据。 我会在你的代码中解释

if (((CheckBox) v).isChecked())

您依赖于视图的 isChecked 方法。 由于android列表回收机制,这可能会导致错误。

您可以在下面的链接中了解它
How ListView's recycling mechanism works

TL;DR 视图正在被回收,因此当您滚动列表时。 因此,例如,用户选中了第 3 项,然后向下滚动列表,例如第 13 项也可能显示为选中,即使它不是。

所以当 onClick 触发时,我们需要将选中状态保存在某个列表中

在理论介绍之后,我将在代码中展示它。

//Here you'll need to create some boolean array or list to store
//checked not checked positions lets call it checked
boolean[] checkedArr = new boolean[catalogDatabases.size()];
// catalogDatabases.size represents your data set size
// note that all items will be false initially

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        /**
         * Other views binding.......
         */
        holder.itemCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                checkedArr[position] = isChecked;
                sumAllCheckedAndNotify();
            }
        }
        );
    }

(我决定对每次检查进行计算,以便更直接地更新事件)

    private void sumAllCheckedAndNotify() {
        double sum = 0;
        for (int i = 0; i < checkedArr.length; i++) {
            if(checkedArr[i]) {
                sum += Double.parseDouble(catalogDatabases.get(i).getPriceItem());
            }
        }
        // pudate the listener
        listener.respond(sum, selectedCount);
    }

【讨论】:

【解决方案2】:

totalPrice 存储在适配器内,并根据CheckBox 状态加/减值,如下所示:

class YourAdapter extends ... {

    double totalPrice = 0;

    ...

    @Override
    public void onBindViewHolder(@NonNull RecyclerHolder holder, int position) {

       holder.itemCheckBox.setChecked(catalogDatabases.get(position).isSelected());

       holder.itemCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double price = Double.parseDouble(catalogDatabases.get(position).getPriceItem());

                boolean isSelected = !catalogDatabases.get(position).isSelected();
                catalogDatabases.get(position).setSelected(isSelected);

                if (isSelected) {
                    totalPrice += price;
                } else {
                    totalPrice -= price;
                }

                holder.listener.respond(totalPrice);

                notifyDataSetChanged();
            }
        });
    }
}

更新您的 模型 以保持选中/未选中状态以保留数据

class CatalogDatabase { //Assume this is your model class

    private boolean isSelected

    ....

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    ....
}

【讨论】:

  • ((CheckBox) v).isChecked() 可能不正确,因为列表视图回收机制
猜你喜欢
  • 2021-05-12
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多