android - 更新现有列表项的数量而不是添加新项

标签 android list android-recyclerview android-adapter

我正在开发一个 POS 管理系统,用户可以在其中从产品列表中选择一个项目,并将所选值添加到检查列表中。单击产品列表后,我可以成功将新项目添加到核对 list ,但无法更新核对 list 项目的数量之前已经添加了。查看图片以获得清晰的演示,

Screenshot

从图像中您可以看到每个产品项目都作为新项目添加到检查列表中,而不是更新数量。

我正在寻找一种解决方案,以在单击产品列表中的项目后更新 list 数量。

这是我的产品列表 recyclerview 的点击监听器,

recyclerProducts.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        // TODO Handle item click

                        // productList is the arraylist used in product list 
                        Products product = productList.get(position);

                        Contents c = new Contents();
                        c.setTitle(product.getName());
                        c.setQuantity(product.getQuantity());
                        orderList.add(c);
                        recyclerListAdapter.notifyDataSetChanged();

                    }
                })
        );

这是检查列表的适配器,

private List<Contents> contentsList;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public TextView quantity;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.txtTitle);
            quantity = (TextView) view.findViewById(R.id.txtQuantity);
        }
    }

    public RecyclerListAdapter(List<Contents> contentsList) {
        this.contentsList = contentsList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_product, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Contents contents = contentsList.get(position);

        holder.title.setText(contents.getTitle());
        holder.quantity.setText("1");
    }

    @Override
    public int getItemCount() {
        return contentsList.size();
    }

最佳答案

应该做这样的事情来代替

for(Contents contents : orderList){
    if (contents.getTitle().equals(product.getName())){
        contents.setQuantity(product.getQuantity())
        recyclerListAdapter.notifyDataSetChanged();
        return;
    }
//assume existing item was not found and add as before
Contents c = new Contents();
c.setTitle(product.getName());
c.setQuantity(product.getQuantity());
orderList.add(c);
recyclerListAdapter.notifyDataSetChanged();
return;

使用 HashMap 作为容器而不是列表是更好的选择,因为我假设产品类型/名称是唯一的。

map 声明。

Map<String,Contents> contentsMap = new HashMap<>();

用法

    Contents contents = contentsMap.get(product.getName());
    if (contents != null){
        contents.setQuantity(product.getQuantity());//update existing quantity
        recyclerListAdapter.notifyDataSetChanged();
    } else{
        Contents newContents = new Contents();
        newContents.setTitle(product.getName());
        newContents.setQuantity(product.getQuantity());
        contentsMap.put(product.getName(),newContents);
        recyclerListAdapter.notifyDataSetChanged();
    }

请注意, HashMap 不需要迭代一堆值来更新数量。当您需要迭代这些值时,您可以像这样使用 map.values() 方法。

for (Contents contents: contentsMap.values()){
     //doSomething with contents, ie. create a view
}

关于android - 更新现有列表项的数量而不是添加新项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582184/

相关文章:

android - 层列表和选择器之间的区别 - Android XML

python - 如何对 pandas 列中的列表执行一次热编码?

python将列表字符串转换为列表列表

html - (新)制作水平列表

android - 处理 RecyclerView 适配器中的按钮单击?

android - 使用 Retrofit 从服务器获取多个 View 项目来创建 RecyclerView

android - 将自定义可打包对象额外或在 ArrayList 中传递给 RemoteViewsService 会中断 appwidget

android - Kotlin Android 创建和共享 CSV 文件

android - 如何在对 recyclerview 项目执行单击时更新 viewModel 的实时数据

java - 尝试使用 ImageLoader 加载本地镜像时出现 NullPointerException