简体   繁体   English

更新哈希图中的字符串值

[英]Update a String value in hashmap

I have a hashmap that will get data every time I selected a row of item from other hashmap. 我有一个哈希图,每次我从其他哈希图中选择一行项目时,该哈希图都会获取数据。 And I have do a checking in arraylist before the data stored into the hashmap. 而且我在将数据存储到哈希图中之前在arraylist中进行了检查。 This checking is to check whether the itemID is exists in arraylist. 此检查是为了检查itemID在arraylist中是否存在。 If yes, I want to change the value of quantity for the itemID that is exist in the arraylist. 如果是,我想更改arraylist中存在的itemID的数量值。 For now, the problem is the quantity never update or changed. 目前,问题在于数量从未更新或更改。

I know this is not the best way for doing it but could any help me to solve this problem? 我知道这不是最好的方法,但是有什么可以帮助我解决这个问题的吗? Thanks in advance. 提前致谢。

This is my code currently 这是我目前的代码

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);
}

The problem is if the key is found you are updating the new map that you created which is not stored in list3. 问题是,如果找到该密钥,则您正在更新创建的新地图,该地图未存储在list3中。

Try following code it will work: 尝试以下代码,它将起作用:

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);
}
System.out.println("start");
HashMap<String,String> a1 = new HashMap<String, String>();
a1.put("a1", "true");
a1.put("a1", "true2");
System.out.println("final value is ==" + a1.get("a1"));

I did this and I get true2 so there is no prob to replace or override value in HashMap , just make some break point and check i think error is some other place may be the value which u want to enter is nor proper may be null check once. 我做到了,我得到true2所以在HashMap没有替换或覆盖值的概率,只是做一些断点并检查我认为错误是其他地方可能是您想要输入的值也不正确可能是null检查一旦。

may be if (exists.equals(itemID)) is always false just debug once 可能是如果(exists.equals(itemID))始终为false,只需调试一次

Just replace value like this 只是这样替换值

int position;

map.get(position).put("String");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM