2013-12-12 42 views
2

一个字符串值,我有一个HashMap,将我选择的项目的一排从其他的HashMap每次得到的数据。在数据存储到散列表之前,我已经在arraylist中进行了检查。此检查是检查数组是否存在于数组列表中。如果是,我想更改数组中存在的itemID的数量值。目前,问题是数量从未更新或改变。更新在HashMap中

我知道这是不是做的最好办法,但可以在任何帮助我解决这个问题呢?提前致谢。

这是我的代码目前

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

价值究竟是什么'LIST3'? – Savv

+0

它的我想列表 –

+0

@SavTheCoder对不起,没有提到它很清楚,LIST3是ArrayList > LIST3; – user3040029

回答

3

的问题是,如果关键是找到要更新您创建的新地图,没有存储在项目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); 
} 
+0

谢谢!它的工作。 – user3040029

+0

@vipul mittal ::好一个兄弟..我会投这个我喜欢它 –

1
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覆盖值,只是做了一些破发点,并检查我认为错误是一些其他地方可能是你想要输入的价值,也可能是空值检查一次。

可能是,如果(exists.equals(ITEMID))始终是假的只是调试一次

+0

@ R.J所以你认为if(exists.equals(itemID))有什么问题? – user3040029

+0

感谢您的测试 – user3040029

+0

@ user3040029 - 我想是的。控制台中是否打印了'c'的值? – SudoRahul

0

只需更换这样

int position; 

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