-1

I'm trying to populate my Listview with Data from my JSON file. but for some reason it outputs just 35 times the same line into the listview. Does anybody know why?

JSONArray json = new JSONArray(data);
                ArrayList<HashMap<String, String>>  arrayList = new ArrayList<HashMap<String, String>>();

                HashMap<String, String> map = new HashMap<String, String>();
                try {
                for(int i=0;i<json.length();i++){
                    JSONObject e =json.getJSONObject(i);
                    map.put("id", String.valueOf(i));
                    map.put("Name", "Vorname: " + e.getString("meta_value"));
                    map.put("orderid", "id: " + e.getString("post_id"));
                    arrayList.add(map);
                }
final ArrayAdapter arrayAdapter = new ArrayAdapter(activity2.this, android.R.layout.simple_list_item_1,arrayList);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        ListView myListView = (ListView) findViewById(R.id.list);
                        myListView.setAdapter(arrayAdapter);
                    }
                });
Nordic Guy
  • 301
  • 5
  • 13
  • 1
    Each time you add map to a list in a loop, you add the same map. If you want a different map for every iteration, you need to create map inside the loop. – M. Prokhorov Oct 16 '19 at 20:20
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – Tom Oct 16 '19 at 20:40

2 Answers2

0

All variables of object type in Java are reference variables, and it's important to distinguish setting a reference variable (using =) from manipulating the object that the variable refers to (using some mutating method).

Here, you've set map to store a reference to your HashMap. Then, in your loop, you add that same reference, map, to arrayList over and over. When you come to examine arrayList, there are multiple copies of map in it, but they're all equal. So you'll just see, over and over, whatever happens to be in the HashMap at that time. You only ever created one HashMap; and every time you put your "id", "Name" and "orderid" into your HashMap, you just overwrite what was there previously.

To fix this, you need to create a new HashMap every time through the loop. What you want to do is to declare map before the loop, but assign it inside the loop, to a new object each time.

HashMap<String, String> map;
try {
    for(int i=0;i<json.length();i++){
        map = new HashMap<String, String>();
        e =json.getJSONObject(i);
        map.put("id", String.valueOf(i));
        map.put("Name", "Vorname: " + e.getString("meta_value"));
        map.put("orderid", "id: " + e.getString("post_id"));
        arrayList.add(map);
    }
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

Currently, you are assigning the same object to each element of the array.

Move the creation of the map within the for loop and use that scoped variable to do manipulation and then add it to the array.

 try {
            for(int i=0;i<json.length();i++){
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e =json.getJSONObject(i);
                map.put("id", String.valueOf(i));
                map.put("Name", "Vorname: " + e.getString("meta_value"));
                map.put("orderid", "id: " + e.getString("post_id"));
                arrayList.add(map);
            }
nishant
  • 91
  • 6