我在ListView
中收到的值无效。我的代码如下。
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 :(得分:1)
相同的地图会多次添加到列表中。
尝试将新的HashMap&lt; ...&gt; -line移动到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);
}
答案 1 :(得分: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”初始化,因此实际上是在覆盖相同的散列映射并将其放回到arraylist中。
尝试在for-loop中移动你的HashMap初始化。
答案 2 :(得分: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);
}