问题
I'm using the MergeAdapter to create a sectioned list where each section of the list contains a different type of underlying data. All is working pretty much as expected. However, the ListActivity's onListItemClicked method seems more complicated than it should be when it comes to determining the type of data underlying the selected list item. This is the case because I can't find a good way to determine the type of object that underlies the selected item unless I keep a separate map of list position to object type. I was hoping that I could use the getItemViewType method to return the same type for similar items, but if those items are in separate sections of the ListView, the MergeAdapter returns a type value that offset by the location of the section, i.e., the same type of item in a different section of the list gets a different type value. I feel like I'm missing something obvious... Any help that anyone can provide would be greatly appreciated.
回答1:
I'm using the MergeAdapter to create a sectioned list where each section of the list contains a different type of underlying data.
As the MergeAdapter project page notes, "If you have questions regarding the use of this code, please join and ask them on the cw-android Google Group".
Fortunately for you, I saw your question here.
This is the case because I can't find a good way to determine the type of object that underlies the selected item unless I keep a separate map of list position to object type.
Call getItem()
on the MergeAdapter
to get the actual object, then use instanceof
or use interfaces or something to find an appropriate type to use on the object itself.
来源:https://stackoverflow.com/questions/4139812/using-mergeadapter-looking-for-a-way-to-determine-a-list-items-data-type-in-th