Java 更新hashmap中的字符串值
我有一个hashmap,每当我从其他hashmap中选择一行项目时,它都会获取数据。在将数据存储到hashmap之前,我已经签入了arraylist。此检查用于检查arraylist中是否存在itemID。如果是,我想更改arraylist中存在的itemID的quantity值。目前,问题是数量从未更新或改变 我知道这不是最好的方法,但能帮我解决这个问题吗?提前谢谢 这是我目前的代码Java 更新hashmap中的字符串值,java,android,android-listview,arraylist,hashmap,Java,Android,Android Listview,Arraylist,Hashmap,我有一个hashmap,每当我从其他hashmap中选择一行项目时,它都会获取数据。在将数据存储到hashmap之前,我已经签入了arraylist。此检查用于检查arraylist中是否存在itemID。如果是,我想更改arraylist中存在的itemID的quantity值。目前,问题是数量从未更新或改变 我知道这不是最好的方法,但能帮我解决这个问题吗?提前谢谢 这是我目前的代码 private void listOrder(String itemValue, String 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);
}
private void列表顺序(String itemValue,String itemID)
{
HashMap=newHashMap();
字符串数量=“1”;
map.put(FOODID3,itemID);
map.put(FOODNAME3,itemValue);
地图放置(食物数量3,数量);
布尔值=false;
如果(!LIST3.isEmpty())
{
对于(int i=0;i
System.out.println(“开始”);
HashMap a1=新的HashMap();
a1.投入(“a1”,“真实”);
a1.投入(“a1”、“2”);
System.out.println(“最终值为==”+a1.get(“a1”);
我这样做了,我得到了true2
,因此没有问题替换或覆盖HashMap
中的值,只需做一些断点并进行检查,我认为错误是其他地方可能是您要输入的值不正确,也可能是空检查一次
可能是如果(exists.equals(itemID))始终为false,只需调试一次System.out.println(“开始”);
HashMap a1=新的HashMap();
a1.投入(“a1”,“真实”);
a1.投入(“a1”、“2”);
System.out.println(“最终值为==”+a1.get(“a1”);
我这样做了,我得到了true2
,因此没有问题替换或覆盖HashMap
中的值,只需做一些断点并进行检查,我认为错误是其他地方可能是您要输入的值不正确,也可能是空检查一次
可能是if(exists.equals(itemID))始终为false只需调试一次问题在于,如果找到密钥,则您正在更新您创建的未存储在列表3中的新映射 请尝试以下代码,它将正常工作:
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 = LIST3.get(i).get(FOODQUANTITY3);
int quantityy = Integer.parseInt(b) +1;
String c = Integer.toString(quantityy);
System.out.println(c);
LIST3.get(i).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);
}
private void列表顺序(String itemValue,String itemID)
{
HashMap=newHashMap();
字符串数量=“1”;
map.put(FOODID3,itemID);
map.put(FOODNAME3,itemValue);
地图放置(食物数量3,数量);
布尔值=false;
如果(!LIST3.isEmpty())
{
对于(int i=0;i
问题在于,如果找到密钥,则您正在更新您创建的未存储在列表3中的新地图
请尝试以下代码,它将正常工作:
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 = LIST3.get(i).get(FOODQUANTITY3);
int quantityy = Integer.parseInt(b) +1;
String c = Integer.toString(quantityy);
System.out.println(c);
LIST3.get(i).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);
}
private void列表顺序(String itemValue,String itemID)
{
HashMap=newHashMap();
字符串数量=“1”;
map.put(FOODID3,itemID);
map.put(FOODNAME3,itemValue);
地图放置(食物数量3,数量);
布尔值=false;
如果(!LIST3.isEmpty())
{
对于(int i=0;i
只需像这样替换值即可
int position;
map.get(position).put("String");
就像这样替换值
int position;
map.get(position).put("String");
什么是
LIST3
?@SavTheCoder抱歉没有明确提到,LIST3是arraylistlist3;您不能使用ArrayList.contains()
检查对象是否存在吗?如果(exists.equals(itemID))块,控件是否进入该块?@R.J是的,一切都正常,只是我无法更新或更改存在的值。LIST3
到底是什么?@SavTheCoder抱歉没有明确提及,LIST3是ArrayList LIST3;您不能使用ArrayList.contains()
检查对象是否存在吗?如果(exists.equals(itemID))
块,控件是否进入块中?@R.J是的,所有内容