Unless you were writing your Android apps under some kind of digital rock, you heard about Mark Murphy a.k.a. commonsguy. Today I’d like to write about a gotcha related to heterogeneous Adapters in general, which recently bit me in the rear when I used (misused?) one of Mark’s Android components – MergeAdapter.
As you can read on the project’s site, “MergeAdapter accepts a mix of Adapters and Views and presents them as one contiguous whole to whatever ListView it is poured into”. This means of course that this is a heterogeneous adapter, i.e. the one which returns integer > 1 from getViewTypeCount()
. The implementation of this method is pretty straightforward – it just iterates through the list of adapters it consists of and returns the sum of getViewTypeCount()
s :
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Everything is fine and dandy if you use the code like this:
1 2 3 4 5 6 7 8 9 10 11 |
|
But sometimes you might want to attach the MergeAdapter to ListView and add fill it later (the real scenario for this case is adding stuff in onLoadFinished
callback, I’m using contrived example for sake of simplicity):
1 2 3 4 5 6 7 8 9 10 11 12 |
|
This code will work as long as the adapter’s contents fit on one screen, but if you start scrolling the list and the item recycling kicks in your app will crash with ClassCastException
from your adapters’ getView()
. If by some chance you use the same IDs for the Views of the same type the app won’t crash, but your items probably won’t look exactly as they should. Either way, you won’t be happy.
The root cause is the undocumented fact that the getViewTypeCount() is called only once after attaching it with ListView.setAdapter()
. In the example above, the MergeAdapter contains only one item type when the setAdapter()
is called, getViewTypeCount()
will return 1, and adding the second adapter with another item type won’t change this.
Why doesn’t this crash right away? The ListView will call getView()
in correct adapters, but then it will try to reuse items created by one adapter for items in the second adapter, because it assumes there is only one view type (getViewTypeCount()
returned 1).
So what’s the lesson here? Do not change the MergeAdapter in loader callbacks, either construct it before setAdapter()
call (for example add empty CursorAdapters
and call CursorAdapter.swapCursor()
later) or postpone the setAdapter()
call until you load all the parts. The more general rule is that you may not calculate the number of item types from the actual data, for example the following code won’t work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|