簡體   English   中英

更新哈希圖中的字符串值

[英]Update a String value in hashmap

我有一個哈希圖,每次我從其他哈希圖中選擇一行項目時,該哈希圖都會獲取數據。 而且我在將數據存儲到哈希圖中之前在arraylist中進行了檢查。 此檢查是為了檢查itemID在arraylist中是否存在。 如果是,我想更改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中。

嘗試以下代碼,它將起作用:

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

我做到了,我得到true2所以在HashMap沒有替換或覆蓋值的概率,只是做一些斷點並檢查我認為錯誤是其他地方可能是您想要輸入的值也不正確可能是null檢查一旦。

可能是如果(exists.equals(itemID))始終為false,只需調試一次

只是這樣替換值

int position;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM