0

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
2
  • @Shark : simpleAdapter is made for list of maps. how is it amazing? Commented Oct 3, 2012 at 14:55
  • whoops, my bad. thought it was more like BaseAdapter... Commented Oct 3, 2012 at 15:04

3 Answers 3

1

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

                }
Sign up to request clarification or add additional context in comments.

Comments

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

                }

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

Comments

0

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

      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.