gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:24:33 25 4
gpt4 key购买 nike

我正在开发一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com