I have a list view adapter that uses different types of view rows.
Most of the time it works fine. But when I remove an element from the list it crashes. It sends a convertView of the incorrect type to getView
public View getView(int position, View convertView, ViewGroup patent) ...
But getItemViewType is returning the correct type.
public int getItemViewType(int position)
so I see something that looks like this
give me the type for position 1 -> returns correct type (say 1)
give me a view for position 1 with a content view for the wrong type (say type 2.)
Any ideas?
That's normal, if you get a
View
with different type inconvertView
, you would create anew View
, and not reuseconvertView
.Probably there are no reusable views with the given type.
Note: This answer is from 2011 and might no longer apply.
Try overiding getViewTypeCount()
Return the number of types of Views that will be created by getView(int, View, ViewGroup). Each type represents a set of views that can be converted in getView(int, View, ViewGroup).
When implemented right, the ListView guarantees that the view provided as the convertView is from the right Type
You should override
getViewTypeCount()
, returning the number of view types you have and overridegetItemViewType(int position)
returning the view type in the range from 0 togetViewTypeCount() - 1
I had the same issue and it resulted in crashes or unexpected behavior.
This how I fixed my issue:
getViewTypeCount() should return the number of different type of rows in the view
getItemViewType(...) should return the type of the item at position
getView(...) should set a enum type on view when inflated