【发布时间】:2020-07-07 00:33:40
【问题描述】:
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("id", maxRowId);
hashMap.put("item", item_name);
hashMap.put("price", price);
hashMap.put("quantity", quantity);
hashMap.put("total", total);
//customItemArrayList.add(hashMap);
// int id = Integer.parseInt(customItemArrayList.get(position).get("id").toString());
for(int i = 0; i < customItemArrayList.size(); i++)
{
//System.out.println(namesList.get(i));
String name = customItemArrayList.get(i).get("item").toString();
if (name.equals(item_name)){
Toast.makeText(getContext(), "It exists",Toast.LENGTH_LONG).show();
i = customItemArrayList.size();
} else {
customItemArrayList.add(hashMap);
//i = customItemArrayList.size();
}
}
maxRowId++;
我有一个带有哈希图的对象 ArrayList。在添加项目之前,我会检查是否已存在具有相似名称的项目。如果存在,则不应再次添加。上面的代码确实表明该项目存在,但是,它继续添加该项目。它可能有什么问题?
【问题讨论】:
-
如果名称不存在,您要添加整个 hashMap 吗?
-
是的,每个条目都是一个hashMap
标签: java android arrays hashmap