arrays - android ListView Simple Adapter, item repeating -


i created custom listview , in java filled listview simpleadapter. here's code:

setcontentview(r.layout.activity_my); list = (listview) findviewbyid(r.id.lvlist);  itemlist = new arraylist<hashmap<string, string>>(); itemmap = new hashmap<string, string>();  (int = 0; < songs.length; i++) {     itemmap.put("song", songs[i]);     itemmap.put("artist", artists[i]);     system.out.println(songs[i] + "     " + artists[i]);     itemlist.add(itemmap); }  simpleadapter adapter = new simpleadapter(this, itemlist,             android.r.layout.simple_list_item_2, new string[] {"song",                     "artist"}, new int[] { android.r.id.text1,                     android.r.id.text2 }); list.setadapter(adapter); 

now created 2 string arrays song , artist respectively , put them in hashmap , put hashmap in arraylist called itemlist. after set adapter default list item id's in parameter.

the problem facing when run application, list view shows last item , subitem of string arrays repeatedly. here's image reference. my listview

i'd know why happening. tried putting itemlist.add(itemmap); outside for() loop still didn't work. appreciated proper explanation, cause i'm kinda new this. thank you!

you can achieve recreating itemmap object @ every iteration. try following way.

itemmap = new hashmap<string, string>();  (int = 0; < songs.length; i++) {     itemmap.put("song", songs[i]);     itemmap.put("artist", artists[i]);     system.out.println(songs[i] + "     " + artists[i]);     itemlist.add(itemmap); } 

change

for (int = 0; < 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 key value pair means store value key. hashmap not allow duplicate key. code youe have added value song , artist. can not add value these keys again. itemlist add itemmap @ every iteration. why list repeating same record. avoid problem re instantiate itemmap object.

i hope you.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -