Reputation: 15078
I am getting invalid value in ListView
. My code is below.
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);
when i run this code i am getting answer like this
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
But I do not understand why I am getting an invalid value in ListView
Like below:
Httpss
Httpss
Httpss
Upvotes: 0
Views: 279
Reputation: 15078
change code with following code it will solve your error.....
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);
}
Upvotes: 0
Reputation: 213203
+-------- 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);
}
Since you have your "HashMap" initialization outside your loop, you are actually overwriting the same hashmap and putting it back to the arraylist..
Try to move your HashMap initialization inside for-loop..
Upvotes: 0
Reputation: 6711
The same map is added to the list several times.
Try moving the new HashMap<...>-line into the for-loop:
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);
}
Upvotes: 1