1

Hi my code only shows the last element of the ArrayList! Heres my code:

    ListView list = (ListView) findViewById(R.id.t1player_statsList);

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

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

    for(Player player : team1.getPlayers())
        map.put("player", player.getName()); 
        map.put("score", "0");
        mylist.add(map);


    SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.mylistrow,
                new String[] {"player", "score"}, new int[] {R.id.NameCell, R.id.ScoreCell});
    list.setAdapter(mSchedule);

i feel that its something to do with my for loop. can someone please help!! thanks in advance!

0

1 Answer 1

6

You are creating only one instance of Hashmap that is over-writing all the values again and again and the last value remains. So, create a new instance inside for loop.

    HashMap<String, String> map;

    for(Player player : team1.getPlayers()){
        map = new HashMap<String, String>();
        map.put("player", player.getName()); 
        map.put("score", "0");
        mylist.add(map);
     }
Sign up to request clarification or add additional context in comments.

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.