android ListView Simple Adapter, item repeating

浪尽此生 提交于 2019-12-06 11:57:57

You can achieve this by recreating your itemMap object at every iteration. Try the following way.

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

for (int i = 0; i < songs.length; i++) {
    itemMap.put("song", songs[i]);
    itemMap.put("artist", artists[i]);
    System.out.println(songs[i] + "     " + artists[i]);
    itemList.add(itemMap);
}

Change this to

for (int i = 0; i < songs.length; i++) {
    itemMap = new HashMap<String, String>();
    itemMap.put("song", songs[i]);
    itemMap.put("artist", artists[i]);
    System.out.println(songs[i] + "     " + artists[i]);
    itemList.add(itemMap);
}

Explaination:

Refer here. Note HashMap is a key value pair which means it will store some value for a key. So HashMap will not allow duplicate key. From your code already youe have added the value for song and artist. So you can not add the value for these keys again. But the itemlist add the itemMap at every iteration. That is why the list repeating the same record. To avoid this problem simply re instantiate the itemMap object.

I hope this will help you.

bro, you won't believe but you are doing wrong.

make a new object of HashMap on each iteration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!