android - How to check if a view is of an appropriate type before using in Adapter getView -
i'm using adapter 3 types of views
public class myadapter extends arrayadapter<myclass> { @override public int getviewtypecount() { return 3; } //... }
and appears wrong types of views passed getview. indeed docs warn it:
note: should check view non-null , of appropriate type before using.
how should check if view of appropriate type before using?
should check findviewbyid if contains id appropriate xml? check "if view of appropriate type"?
edit: answers far seems not miss question clarify: yeah i'm using getitemviewtype
have 3 types of views, convertview
in getview has wrong type (not view inflated getitemviewtype
) , cannot converted right 1 - question not how check view should returned (and covered "bigdestroyer" answer), if view passed getview can reused (and in case cannot).
"appropriate type" means have check in getview
method type of convertview
in order return custom view
or one.
you can override getitemviewtype
method of baseadapter
. instance:
public int getitemviewtype(int position) { myitem item = getitem(position); return item.getviewtype(); }
and:
public view getview(int position, view convertview, viewgroup parent) { int viewtype = getitemviewtype(position); if (convertview != null) { if (viewtype == 1) return recycleviewoftype1(position, convertview); if (viewtype == 2) return recycleviewoftype2(position, convertview); } else { if (viewtype == 1) return newviewoftype1(position); if (viewtype == 2) return newviewoftype2(position); } return null; }
Comments
Post a Comment