java - IndexOutOfBoundsException in RecyclerAdapter with Multiple View Types -
i'm getting indexoutofboundsexception when attempting return view type function of position of data in array. have 2 sources of data. 1 advertisement, , other list of names , locations. need combine arrays 1 single array before processing in recycleradapter? i'd rather not, if in principle different types, i.e. images versus text. getitemcount()
method returns summed size of 2 arrays. why getting exception? how can achieve desired result of showing data 2 different sources in 2 different view types?
my main activity:
public class mainactivity extends appcompatactivity { private arraylist<object> getpresidentsarraylist() { arraylist<object> items = new arraylist<>(); items.add(new president("george washington", "mount vernon")); items.add(new president("john adams", "braintree")); items.add(new president("thomas jefferson", "monticello")); items.add(new president("james madison", "port conway")); return items; } private arraylist<object> getadsarraylist() { arraylist<object> ads = new arraylist<>(); ads.add(new sponsored("craft beer 20% off", "budweiser")); ads.add(new sponsored("craft beer 20% off", "budweiser")); ads.add(new sponsored("craft beer 20% off", "budweiser")); ads.add(new sponsored("craft beer 20% off", "budweiser")); return ads; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); recyclerview.setlayoutmanager(new linearlayoutmanager(this)); binddatatoadapter(recyclerview); } private void binddatatoadapter(recyclerview recyclerview) { recyclerview.setadapter(new heterogeneousrecycleradapter(getpresidentsarraylist(), getadsarraylist())); } }
my recycler adapter allowing multiple views:
public class heterogeneousrecycleradapter extends recyclerview.adapter<recyclerview.viewholder> { private list<object> items; private list<object> ads; private final int president = 0, sponsored = 1; public heterogeneousrecycleradapter(list<object> items, list<object> ads) { this.items = items; this.ads = ads; } @override public int getitemcount() { return (this.items.size() + this.ads.size()); } @override public int getitemviewtype(int position) { if (items.get(position) instanceof president) { return president; } else if (ads.get(position) instanceof sponsored) { return sponsored; } return -1; } @override public recyclerview.viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) { recyclerview.viewholder viewholder; layoutinflater inflater = layoutinflater.from(viewgroup.getcontext()); switch (viewtype) { case president: view v1 = inflater.inflate(r.layout.layout_viewholder1, viewgroup, false); viewholder = new viewholder1(v1); break; case sponsored: view v2 = inflater.inflate(r.layout.layout_viewholder2, viewgroup, false); viewholder = new viewholder2(v2); break; default: view v = inflater.inflate(android.r.layout.simple_list_item_1, viewgroup, false); viewholder = new recyclerviewsimpletextviewholder(v); break; } return viewholder; } @override public void onbindviewholder(recyclerview.viewholder viewholder, int position) { switch (viewholder.getitemviewtype()) { case president: viewholder1 vh1 = (viewholder1) viewholder; configureviewholder1(vh1, position); break; case sponsored: viewholder2 vh2 = (viewholder2) viewholder; configureviewholder2(vh2, position); break; default: recyclerviewsimpletextviewholder vh = (recyclerviewsimpletextviewholder) viewholder; configuredefaultviewholder(vh, position); break; } } private void configuredefaultviewholder(recyclerviewsimpletextviewholder vh, int position) { vh.getlabel().settext((charsequence) items.get(position)); } private void configureviewholder1(viewholder1 vh1, int position) { president president = (president) items.get(position); if (president != null) { vh1.getlabel1().settext(president.getname()); vh1.getlabel2().settext(president.gethometown()); } } private void configureviewholder2(viewholder2 vh2, int position) { sponsored sponsored = (sponsored) ads.get(position); if (sponsored != null) { vh2.getlabel1().settext(sponsored.getname()); vh2.getlabel2().settext(sponsored.getcompany()); } } }
you need create common list adding both list. following -
public heterogeneousrecycleradapter(list<object> items, list<object> ads) { this.items = items; items.addall(ads); }
now change item count -
@override public int getitemcount() { return this.items.size(); }
hope work fine :)
Comments
Post a Comment