【问题标题】:Update a String value in hashmap更新哈希图中的字符串值
【发布时间】: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


【解决方案1】:

问题是如果找到密钥,您正在更新您创建的未存储在 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);
}

【讨论】:

  • @vipul mittal :: 好兄弟.. 我会投票赞成这个我喜欢它
【解决方案2】:
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 中的值,只需做一些断点并检查我认为错误是其他地方可能是你想要输入的值is nor proper 可能是一次空检查。

可能是如果 (exists.equals(itemID)) 始终为假,只需调试一次

【讨论】:

  • @R.J 所以你认为 if (exists.equals(itemID)) 有问题?
  • 感谢您的测试
  • @user3040029 - 我想是的。 c 的值是否打印在控制台中?
  • @R.J 是的,即使 b 的值也正确打印在控制台中
  • 哦,只要将 HashMap 声明为全局,只要这样做我认为它就会解决
【解决方案3】:

只需像这样替换值

int position;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多