2012-10-03 103 views
0

我在ListView中得到無效值。我的代碼如下。從ArrayList得到無效值<HashMap <String,String >>

final ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String, String>>(); 

     runOnUiThread(new Runnable() { 
      public void run() { 
       try { 
        HashMap<String, String> map = new HashMap<String, String>(); 

        for (int i = 0; i < dealList.size(); i++) { 
         map.put(DealItem.DEAL_ID, dealList.get(i).deal_id); 
         map.put(DealItem.DEAL_NAME, dealList.get(i).deal_name); 
         System.out.println("DEAL NAME = "+dealList.get(i).deal_name); 
         placesListItems.add(map); 

        } 

        new PlacesMapActivity(SinglePlaceActivity.this, lon, lat, name, add); 

        ListView lv = (ListView) findViewById(R.id.listView); 
        simpleAdapter = new SimpleAdapter(SinglePlaceActivity.this, placesListItems, R.layout.list_item_deal, new String[] { 
          DealItem.DEAL_ID, DealItem.DEAL_NAME }, new int[] { R.id.reference, R.id.name }); 
        lv.setAdapter(simpleAdapter); 

當我運行這段代碼我得到的答案是這樣

10-03 18:37:29.429: I/System.out(3957): DEAL NAME = Sinbad Cafe 
    10-03 18:37:52.369: I/System.out(3957): DEAL NAME = Coffee,Tea, Hot Milk 
    10-03 18:37:55.189: I/System.out(3957): DEAL NAME = Httpss 

但我不明白爲什麼我在ListView

得到一個無效的值象下面這樣:

Httpss 
    Httpss 
    Httpss 
+0

@Shark:simpleAdapter是* *製成的地圖列表。這太神奇了? – njzk2

+0

哎呦,我的壞。認爲它更像BaseAdapter ... – Shark

回答

0
+-------- HashMap<String, String> map = new HashMap<String, String>(); 
    | 
    |   for (int i = 0; i < dealList.size(); i++) { 
    +---------------> 
        map.put(DealItem.DEAL_ID, dealList.get(i).deal_id); 
        map.put(DealItem.DEAL_NAME, dealList.get(i).deal_name); 
        System.out.println("DEAL NAME = "+dealList.get(i).deal_name); 
        placesListItems.add(map); 

       } 

既然你有你的「HashMap的」初始化你的循環之外,你實際上是覆蓋相同HashMap和把它背到ArrayList ..

嘗試移動你的HashMap的初始化內部for循環..

1

多次將相同的地圖添加到列表中。

嘗試將新的HashMap < ...> - 行到for循環:

   for (int i = 0; i < dealList.size(); i++) { 
     -->  HashMap<String, String> map = new HashMap<String, String>(); 
        map.put(DealItem.DEAL_ID, dealList.get(i).deal_id); 
        map.put(DealItem.DEAL_NAME, dealList.get(i).deal_name); 
        System.out.println("DEAL NAME = "+dealList.get(i).deal_name); 
        placesListItems.add(map); 

       } 
0

變化代碼下面的代碼將解決您的錯誤.....

try { 


       for (int i = 0; i < dealList.size(); i++) { 
       HashMap<String, String> map = new HashMap<String, String>(); 
        map.put(DealItem.DEAL_ID, dealList.get(i).deal_id); 
        map.put(DealItem.DEAL_NAME, dealList.get(i).deal_name); 
        System.out.println("DEAL NAME = "+dealList.get(i).deal_name); 
        placesListItems.add(map); 

     } 
相關問題