简体   繁体   中英

Got invalid value from ArrayList<HashMap<String, String>>

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

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);

                }
   +-------- 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..

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);

      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM