Skip to content Skip to sidebar Skip to footer

Android - Listview Adapter With Items Of Two Views

I'm new at Android programming and I'm stuck in a situation for two days that I can't seem to solve. I've got a ListActivity that should be populated with two different views that

Solution 1:

You can subclass BaseAdapter and utilize the following methods:

getItemViewType(int position)

getViewTypeCount()

getViewTypeCount() should return the number of different row types your adapter supports. getItemViewType() is where you implement the "decision" of which view type a particular row should have. If, for example, getViewTypeCount() returns 3, then getItemViewType() method should return 0, 1, or 2.

You can use this inside of getView to inflate/create a different layout for different row types.

EDIT:

Since it's a custom adapter, you can implement it in whatever way makes sense for your data. If you can create a data structure that works for what you want, then just make the adapter able to work with that. In the worst case, you might just have an array (or list) of Objects and have to use instanceof to do the decision work.

Post a Comment for "Android - Listview Adapter With Items Of Two Views"