android - ClassCastException / ArrayIndexOutOfBoundsException by trying to make a RecyclerView to handle multiple types of viewholder -
i've problem recyclerview. let me explain problem in detail. try implement recylerview 4 different kinds of child views. followed how-to "double wong". have 4 viewholders extended 1 main viewholder, @ oncreateviewholder() choose 1 of them should initialized. override getitemviewtype() method. works fine, long childs of type 0. (type 0 means item not in database, type 1 - 3 means different flags in database).
sometimes arrayindexoutofboundsexception , classcastexception, i cant figure out pattern. here comes code, made cuts make shorter.
the fragment
public class sucheserienfragment extends fragment { ... public list<pair<serieinfo, integer>> listtvshows; private seriecardviewsucheadapter seriecardviewsucheadapter; ... private class loadserie extends asynctask<string, void, string> { @override protected string doinbackground(string... search) { ... int type; if (db.checkserie(serieinfo)) { serieinfo = db.getseriebyname(serieinfo.getname()); if (serieinfo.getarchivflag() == 0){ type = suchemainviewholder.type_at_watchlist; } else if (serieinfo.getarchivflag() == 1) { type = suchemainviewholder.type_pausiert; } else if (serieinfo.getarchivflag() == 2) { type = suchemainviewholder.type_abgeschlossen; } else { type = suchemainviewholder.type_search; } } else { type = suchemainviewholder.type_search; } final serieinfo finalserieinfo = serieinfo; final int finaltype = type; getactivity().runonuithread(new runnable() { @override public void run() { listtvshows.add(new pair(finalserieinfo, finaltype)); seriecardviewsucheadapter.notifyiteminserted(seriecardviewsucheadapter.getitemcount() - 1); } }); } } } catch (tvdboutboundconnectionexception e1) { log.e(tag, "es konnte keine verbindung aufgebaut werden"); log.e(tag, "error" + e1); e1.printstacktrace(); } return "executed"; } @override protected void onpostexecute(string string) { log.d(tag, "onpostexecute"); progressbarloadserie.setvisibility(view.gone); if (listtvshows.isempty()) { toast.maketext(getactivity(), "keine serie gefunden", toast.length_short).show(); nothingfoundframelayout.setvisibility(view.visible); } } @override protected void onpreexecute() { log.d(tag, "onpreexecute"); progressbarloadserie.setvisibility(view.visible); } @override protected void onprogressupdate(void... values) { log.d(tag, "onprograessupdate"); } }
suchemainviewholder
public class suchemainviewholder extends recyclerview.viewholder{ public static final int type_search = 0; public static final int type_at_watchlist = 1; public static final int type_pausiert= 2; public static final int type_abgeschlossen= 3; public suchemainviewholder(view itemview) { super ( itemview ); } }
sucheabgeschlossenholder
public class sucheabgeschlossenholder extends suchemainviewholder { public textview tvserienname; public textview tvcurrentepisode; public imageview tvserienimage; public relativelayout linearbackground; public imageview imgviewserie; public imagebutton playbutton; public imagebutton deletebutton; public imagebutton abschliesbutton; public sucheabgeschlossenholder(view v) { super(v); tvserienname = (textview) v.findviewbyid(r.id.tvserienname); tvcurrentepisode = (textview) v.findviewbyid(r.id.tvserienoverview); tvserienimage = (imageview) v.findviewbyid(r.id.imgviewserie); linearbackground = (relativelayout) v.findviewbyid(r.id.rla); playbutton = (imagebutton) v.findviewbyid(r.id.playimagebutton); deletebutton = (imagebutton) v.findviewbyid(r.id.deleteimagebutton); abschliesbutton = (imagebutton) v.findviewbyid(r.id.abschliesenimagebutton); imgviewserie = (imageview) v.findviewbyid(r.id.imgviewserie); } }
suchepausiertholder
suchepausiertholder extends suchemainviewholder { public textview tvserienname; public textview tvcurrentepisode; public imageview tvserienimage; public relativelayout linearbackground; public imageview imgviewserie; public imagebutton playbutton; public imagebutton deletebutton; public imagebutton abschliesbutton; public suchepausiertholder(view v) { super(v); tvserienname = (textview) v.findviewbyid(r.id.tvserienname); tvcurrentepisode = (textview) v.findviewbyid(r.id.tvserienoverview); tvserienimage = (imageview) v.findviewbyid(r.id.imgviewserie); linearbackground = (relativelayout) v.findviewbyid(r.id.rla); playbutton = (imagebutton) v.findviewbyid(r.id.playimagebutton); deletebutton = (imagebutton) v.findviewbyid(r.id.deleteimagebutton); abschliesbutton = (imagebutton) v.findviewbyid(r.id.abschliesenimagebutton); imgviewserie = (imageview) v.findviewbyid(r.id.imgviewserie); }
}
sucheserieviewholder
public class sucheserieviewholder extends suchemainviewholder{ public textview tvserienname; public textview tvserienoverview; public textview tvrating; public imageview tvserienimage; public imagebutton btnaddserie; public linearlayout linearseriensuchebackground; public sucheserieviewholder(view v) { super(v); tvserienname = (textview) v.findviewbyid(r.id.tvserienname); tvserienoverview = (textview) v.findviewbyid(r.id.tvserienoverview); tvserienimage = (imageview) v.findviewbyid(r.id.imgviewserie); btnaddserie = (imagebutton) v.findviewbyid(r.id.btnaddserie); linearseriensuchebackground = (linearlayout) v.findviewbyid(r.id.linearseriensuche); tvrating = (textview) v.findviewbyid(r.id.tvrating); } }
suchewatchedholder
public class suchewatchedholder extends suchemainviewholder { public textview tvserienname; public textview tvcurrentepisode; public imageview tvserienimage; public view btnnext; public relativelayout linearbackground; public imageview imgviewserie; public suchewatchedholder(view v) { super(v); tvserienname = (textview) v.findviewbyid(r.id.tvserienname); tvcurrentepisode = (textview) v.findviewbyid(r.id.tvserienoverview); tvserienimage = (imageview) v.findviewbyid(r.id.imgviewserie); btnnext = (view) v.findviewbyid(r.id.btnnext); linearbackground = (relativelayout) v.findviewbyid(r.id.rla); imgviewserie = (imageview) v.findviewbyid(r.id.imgviewserie); } }
seriecardviewsucheadapter
@override public viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) >{ layoutinflater minflater = layoutinflater.from ( >viewgroup.getcontext () ); int type = getitemviewtype(viewtype); switch (type) { case suchemainviewholder.type_search: viewgroup vsuche = ( viewgroup ) minflater.inflate ( >r.layout.cardview_serie_suche, viewgroup, false ); sucheserieviewholder vhsuche = new sucheserieviewholder ( >vsuche ); return vhsuche; default: vsuche = ( viewgroup ) minflater.inflate ( >r.layout.cardview_serie_suche, viewgroup, false ); vhsuche = new sucheserieviewholder ( vsuche ); return vhsuche; case suchemainviewholder.type_at_watchlist: viewgroup vsuchewatched = ( viewgroup ) minflater.inflate ( >r.layout.cardview_serie, viewgroup, false ); suchewatchedholder vhsuchewatched = new suchewatchedholder >( vsuchewatched ); return vhsuchewatched; case suchemainviewholder.type_pausiert: viewgroup vsuchepausiert = ( viewgroup ) minflater.inflate >( r.layout.cardview_serie_pausiert, viewgroup, false ); suchepausiertholder vhsuchepausiert = new >suchepausiertholder ( vsuchepausiert ); return vhsuchepausiert; case suchemainviewholder.type_abgeschlossen: viewgroup vsucheabgeschlossen = ( viewgroup ) >minflater.inflate ( r.layout.cardview_serie_abgeschlossen, viewgroup, false ); sucheabgeschlossenholder vhsucheabgeschlossen= new >sucheabgeschlossenholder( vsucheabgeschlossen ); return vhsucheabgeschlossen; } } ... @override public void onbindviewholder(viewholder viewholder, final int position) { final serieinfo serieinfo = listserieinfo.get(position).first; if (viewholder == null) { return; } switch (viewholder.getitemviewtype()) { default: ... return; case suchemainviewholder.type_search: ... return; case suchemainviewholder.type_at_watchlist: ... return; case suchemainviewholder.type_pausiert: ... return; case suchemainviewholder.type_abgeschlossen: ... return; } } ... @override public int getitemviewtype(int position) { return listserieinfo.get(position).second; }
indexoutofboundsexception
06-01 00:49:17.327 23289-23289/com.turnonandroid.seriencounter e/androidruntime﹕ fatal exception: main process: com.turnonandroid.seriencounter, pid: 23289 java.lang.indexoutofboundsexception: invalid index 1, size 1 @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) @ java.util.arraylist.get(arraylist.java:308) @ com.turnonandroid.serienmanager.adapter.seriecardviewsucheadapter.getitemviewtype(seriecardviewsucheadapter.java:390) @ com.turnonandroid.serienmanager.adapter.seriecardviewsucheadapter.oncreateviewholder(seriecardviewsucheadapter.java:359) @ android.support.v7.widget.recyclerview$adapter.createviewholder(recyclerview.java:5062) @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:4368) @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:4278) @ android.support.v7.widget.linearlayoutmanager$layoutstate.next(linearlayoutmanager.java:1947) @ android.support.v7.widget.linearlayoutmanager.layoutchunk(linearlayoutmanager.java:1359) @ android.support.v7.widget.linearlayoutmanager.fill(linearlayoutmanager.java:1322) @ android.support.v7.widget.linearlayoutmanager.onlayoutchildren(linearlayoutmanager.java:556) @ android.support.v7.widget.recyclerview.dispatchlayout(recyclerview.java:2673) @ android.support.v7.widget.recyclerview.onlayout(recyclerview.java:2971) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.relativelayout.onlayout(relativelayout.java:1070) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.relativelayout.onlayout(relativelayout.java:1070) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.framelayout.layoutchildren(framelayout.java:335) @ android.widget.framelayout.onlayout(framelayout.java:272) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.framelayout.layoutchildren(framelayout.java:335) @ android.widget.framelayout.onlayout(framelayout.java:272) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.relativelayout.onlayout(relativelayout.java:1070) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.framelayout.layoutchildren(framelayout.java:335) @ android.widget.framelayout.onlayout(framelayout.java:272) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.support.v7.internal.widget.actionbaroverlaylayout.onlayout(actionbaroverlaylayout.java:493) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.framelayout.layoutchildren(framelayout.java:335) @ android.widget.framelayout.onlayout(framelayout.java:272) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.linearlayout.setchildframe(linearlayout.java:1707) @ android.widget.linearlayout.layoutvertical(linearlayout.java:1561) @ android.widget.linearlayout.onlayout(linearlayout.java:1470) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.framelayout.layoutchildren(framelayout.java:335) @ android.widget.framelayout.onlayout(framelayout.java:272) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.support.v4.widget.drawerlayout.onlayout(drawerlayout.java:907) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.widget.framelayout.layoutchildren(framelayout.java:335) @ android.widget.framelayout.onlayout(framelayout.java:272) @ android.view.view.layout(view.java:16552) @ android.view.viewgroup.layout(viewgroup.java:5355) @ android.view.viewrootimpl.performlayout(viewroo
classcastexception
06-01 00:56:09.436 27453-27453/com.turnonandroid.seriencounter e/androidruntime﹕ fatal exception: main process: com.turnonandroid.seriencounter, pid: 27453 java.lang.classcastexception: com.turnonandroid.serienmanager.sucheserieviewholder cannot cast com.turnonandroid.serienmanager.suchewatchedholder @ com.turnonandroid.serienmanager.adapter.seriecardviewsucheadapter.onbindviewholder(seriecardviewsucheadapter.java:167) @ android.support.v7.widget.recyclerview$adapter.bindviewholder(recyclerview.java:5084) @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:4385) @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:4278) @ android.support.v7.widget.linearlayoutmanager$layoutstate.next(linearlayoutmanager.java:1947) @ android.support.v7.widget.linearlayoutmanager.layoutchunk(linearlayoutmanager.java:1359) @ android.support.v7.widget.linearlayoutmanager.fill(linearlayoutmanager.java:1322) @ android.support.v7.widget.linearlayoutmanager.scrollby(linearlayoutmanager.java:1150) @ android.support.v7.widget.linearlayoutmanager.scrollverticallyby(linearlayoutmanager.java:1007) @ android.support.v7.widget.recyclerview.scrollbyinternal(recyclerview.java:1355) @ android.support.v7.widget.recyclerview.ontouchevent(recyclerview.java:2209) @ android.view.view.dispatchtouchevent(view.java:9244) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2536) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2229) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2542) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2243) @ com.android.internal.policy.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:2392) @ com.android.internal.policy.phonewindow.superdispatchtouchevent(phonewindow.java:1730) @ android.app.activity.dispatchtouchevent(activity.java:2710) @ android.support.v7.internal.view.windowcallbackwrapper.dispatchtouchevent(windowcallbackwrapper.java:59) @ com.android.internal.policy.phonewindow$decorview.dispatchtouchevent(phonewindow.java:2353) @ android.view.view.dispatchpointerevent(view.java:9439) @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:4181) @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:4047) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3593) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3646)
thank reading , thinking problem.
ok, indexoutofboundsexception
, should check getitemcount()
in adapter. if calling listserieinfo.get(position)
in getviewtype()
, method should this:
return listserieinfo != null ? listserieinfo.size() : 0;
about classcastexception
, have move default
option in onbindviewholder
bottom. should there.
Comments
Post a Comment