erdomester
erdomester

Reputation: 11839

How to populate a hashmap from an array

i have two arrays (actually one, but i created two for each columns). I want to populate a hashmap with the values for a listview but all elements of the listview is the last element of the arrays:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();

    for (int i=0; i<13; i++)
    {
        map.put("left1", date[i]);
        map.put("right1", name[i]);
        mylist.add(map);
    }


SimpleAdapter  simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
        new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);

Any ideas? Thanks

Upvotes: 3

Views: 11236

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234857

You're adding the same map to every slot of the array. Try this instead:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

for (int i=0; i<13; i++)
{
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("left1", date[i]);
    map.put("right1", name[i]);
    mylist.add(map);
}

Upvotes: 7

Related Questions