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.