简体   繁体   中英

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. This checking is to check whether the itemID is exists in arraylist. If yes, I want to change the value of quantity for the itemID that is exist in the arraylist. 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.

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.

may be if (exists.equals(itemID)) is always false just debug once

Just replace value like this

int position;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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