Using MergeAdapter; looking for a way to determine a list item's data type in the ListActivity onListItemClicked method

烂漫一生 提交于 2020-01-03 20:22:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!