android - 如何检查recyclerView中是否存在新添加的项目?
问题描述
实际上我正在开发一个库存应用程序,我在其中扫描一些 EAN 代码,然后我插入一个项目的数量,然后我将它添加到 recyclerView 中的 ArrayList。
现在我没有问题,因为我已经制作了库存部分,其中项目必须在 recyclerView 中为每个项目有不同的行,但现在我必须制作订单部分,如果 ArrayList 中还存在一个项目,我必须总结它的数量并将其放在recyclerView 的顶部。
当我要添加一个新项目并检查它是否存在于 ArrayList 中时,我试图制作类似 for 循环的东西,如果它存在,我打算将该项目数量与旧项目相加,但问题是有时应用程序会崩溃,并且该项目不会在 recyclerView 之上。
你对我该怎么做有什么建议吗?
for (ItemModel item : itemModel) {
if (item.getCodiceArticolo()
.equals(code.getText()
.toString())) {
item.setQta(String.valueOf(
Integer.parseInt(item.getQta()) + 1));
}
}
我试图做类似的东西。
解决方案
试试这个代码:
ItemModel matchedItem = null;
int matchedItemIndex = -1;
for (ItemModel item : itemModel) {
if (item.getCodiceArticolo()
.equals(code.getText()
.toString())) {
item.setQta(String.valueOf(
Integer.parseInt(item.getQta()) + 1));
matchedItem = item;
matchedItemIndex = itemModel.indexOf(item);
break;
}
}
if (matchedItemIndex > -1) {
itemModel.remove(matchedItem);
itemModel.add(
0,
matchedItem);
notifyItemMoved(index,
0);
}
推荐阅读
- python - 如何在展平数据框列后有选择地将值放入数据框列?
- conda - 删除名称无效的 conda 环境?
- python - 基于两列匹配值的日期差异 - Pandas
- sql-server - 在表中插入值时自动需要表中的当前登录名
- mysql - 如何添加两个 sum case 语句的结果?
- sql - 在 SQL Server 的日期时间中插入 dd/mm/yyyy hh:mm:ss.000
- java - 将 ArrayList 的内容输出为逗号分隔的字符串
- javascript - 每个性能问题的数组比较
- html - 如何定位价值?
- sql - 如何使用错误记录将多个 csv 文件导入 Exasol