android - Custom listView shows less items in random order -
i have made custom inflated listview android app , have problem getview() method.
public class binderdata extends baseadapter { private layoutinflater inflater; private arraylist targetlist; private viewholder holder; private activity nikola; private context context; private static arraylist genres = new arraylist(); public binderdata(activity act, list<genre> map) { targetlist = new arraylist(); (int = 0; i<map.size(); i++){ genre genre = map.get(i); targetlist.add(genre); arraylist<artist> alist = genre.getartists(); (int j = 0; j<alist.size(); j++){ targetlist.add(alist.get(j)); } } this.nikola = act; inflater = (layoutinflater) act .getsystemservice(context.layout_inflater_service); }
and here getview() method
public view getview(int position, view convertview, viewgroup parent) { view vi = convertview; if (convertview == null){ viewableitem item = (viewableitem)targetlist.get(position); if (item.istypeof().equalsignorecase("genre")){ vi = inflater.inflate(r.layout.category_row, null); holder = new genreviewholder(); ((genreviewholder)holder).textviewgenretitle = (textview)vi.findviewbyid(r.id.textviewcategorytitle); string s = ((genre)item).gettitle(); holder.settitle(s); }else{ vi = inflater.inflate(r.layout.list_row, null); holder = new artistviewholder(); ((artistviewholder)holder).textviewartistname = (textview)vi.findviewbyid(r.id.textviewauthornamehome); string s = ((artist)item).getartist(); holder.setartistname(s); } vi.settag(holder); }else{ holder = (viewholder)vi.gettag(); } return vi; }
i have 2 type of layouts list. 1 category title , other 1 items under category.
http://imgur.com/xbpfgti - here picture
i have made targetlist filled data need show in listview. (in constructor) , there problem! artists in list keep repeating , categories not in right place.
have ideas?
your problem due fact not binding data rows when convertview != null
, instead returning convertview
recycled you, why see same repeated data.
you need bind data row's view
s every row, according data type, this:
public view getview(int position, view convertview, viewgroup parent) { final view row; boolean isgenrerow = getitemviewtype(position) == genre_row; // inflate views if new row if (convertview == null) { // right type of view depending on type of data if (isgenrerow) { row = inflater.inflate(r.layout.category_row, null); holder = new genreviewholder(); ((genreviewholder) holder).textviewgenretitle = (textview) row.findviewbyid(r.id.textviewcategorytitle); } else { row = inflater.inflate(r.layout.list_row, null); holder = new artistviewholder(); ((artistviewholder) holder).textviewartistname = (textview) row.findviewbyid(r.id.textviewauthornamehome); } row.settag(holder); } // otherwise, use recycled row else { row = convertview; holder = (viewholder) row.gettag(); } // bind data views, depending on data type viewableitem item = (viewableitem) targetlist.get(position); if (isgenrerow) { ((genreviewholder) holder).settitle(((genre) item).gettitle()); } else { ((artistviewholder) holder).setartistname(((artist) item).getartist()); } return row; }
you must override few other methods let baseadapter
know there more 1 data type, knows how recycle row's view
s.
private static final int genre_row = 0; private static final int artist_row = 1; @override public int getitemviewtype(int position) { // return row type @ position viewableitem item = (viewableitem) targetlist.get(position); return item.istypeof().equalsignorecase("genre") ? genre_row : artist_row; } @override public int getviewtypecount() { // return number of row types there return 2; }
you should able copy , paste solution in, assuming didn't make mistakes.
Comments
Post a Comment