更新现有列表项的数量而不是添加新项
Update existing list item's quantity instead of adding a new item
我正在开发一个 POS 管理系统,用户可以在其中从产品列表中选择一个项目,并将所选值添加到检查列表。单击 产品列表 后,我可以成功地将新项目添加到 清单 ,但无法更新 清单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;
使用哈希图作为容器而不是列表是更好的选择,因为我假设产品 types/names 是唯一的。
地图声明。
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();
}
请注意,哈希映射不需要迭代一堆值来更新数量。当你需要遍历值时,你可以像这样使用 map.values() 方法。
for (Contents contents: contentsMap.values()){
//doSomething with contents, ie. create a view
}
我想你想在这里做的是在你的购物车(装有你的物品的容器)中,你想知道物品 ID,因为你已经知道原始列表中的物品 ID,你可以每次检查您添加一个新项目(如果它已经存在),如果存在,则更新它而不是添加新项目。
我假设您将数据(针对每个订单)存储在带有列的本地 table 中。所以你只需要查询那个特定的项目并更新它是否已经存在。
只是我的两分钱,希望对您有所帮助!
祝你好运,编码愉快!
我正在开发一个 POS 管理系统,用户可以在其中从产品列表中选择一个项目,并将所选值添加到检查列表。单击 产品列表 后,我可以成功地将新项目添加到 清单 ,但无法更新 清单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;
使用哈希图作为容器而不是列表是更好的选择,因为我假设产品 types/names 是唯一的。
地图声明。
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();
}
请注意,哈希映射不需要迭代一堆值来更新数量。当你需要遍历值时,你可以像这样使用 map.values() 方法。
for (Contents contents: contentsMap.values()){
//doSomething with contents, ie. create a view
}
我想你想在这里做的是在你的购物车(装有你的物品的容器)中,你想知道物品 ID,因为你已经知道原始列表中的物品 ID,你可以每次检查您添加一个新项目(如果它已经存在),如果存在,则更新它而不是添加新项目。
我假设您将数据(针对每个订单)存储在带有列的本地 table 中。所以你只需要查询那个特定的项目并更新它是否已经存在。
只是我的两分钱,希望对您有所帮助!
祝你好运,编码愉快!