【发布时间】:2013-12-12 09:55:08
【问题描述】:
我有一个哈希图,每次我从其他哈希图中选择一行项目时都会获取数据。在将数据存储到哈希图中之前,我已经检查了 arraylist。这个检查是检查 itemID 是否存在于 arraylist 中。如果是,我想更改数组列表中存在的 itemID 的数量值。目前,问题是数量永远不会更新或改变。
我知道这不是最好的方法,但可以帮助我解决这个问题吗?提前致谢。
这是我目前的代码
private void listOrder(String itemValue, String itemID)
{
HashMap<String, String> map = new HashMap<String, String>();
String quantity = "1";
map.put(FOODID3, itemID);
map.put(FOODNAME3, itemValue);
map.put(FOODQUANTITY3, quantity);
boolean found = false;
if (!LIST3.isEmpty())
{
for (int i = 0; i < LIST3.size(); i++)
{
String exists = null;
exists = LIST3.get(i).get(FOODID3).toString();
if (exists.equals(itemID))
{
found=true;
String b = map.get(FOODQUANTITY3);
int quantityy = Integer.parseInt(b) +1;
String c = Integer.toString(quantityy);
System.out.println(c);
map.put(FOODQUANTITY3, c); // I can't update the value here
break;
}
}
}
if (!found)
{
LIST3.add(map);
}
LISTORDER = (ListView) findViewById(R.id.listOrder);
List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
LISTORDER.setAdapter(adapter);
}
【问题讨论】:
-
LIST3到底是什么? -
@SavTheCoder 抱歉没说清楚,LIST3 是 ArrayList
> LIST3; -
你不能使用
ArrayList.contains()来检查对象的存在吗? developer.android.com/reference/java/util/… -
控件是否进入此
if (exists.equals(itemID))块? -
@R.J 是的,一切都很好,只是我无法更新或更改存在的值。
标签: java android android-listview arraylist hashmap