user2558461
user2558461

Reputation: 281

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 has almost no simularity. Untill now I've used MergeAdapter and it worked fine but my outcome was like this: Adapter1,Adapter2. and I didn't have the ability to change the order of the items coming to the list. My question is: is it possible to create an adapter that is holding items of two views and not adapters of two views so I'll have my items sorted by the way I input them? For simplicity sake, I got an ArrayList of those two items and each has "int position" so I'll be able to insert them into the list sorted by position.
EDIT: I've tried extending BaseAdapter but once again I need two adapters as I've read online but if I do so, I won't be able to control the place of an item on the list. I hope I'm more clear this time. Welcoming any response.
Thank you.

Upvotes: 0

Views: 709

Answers (1)

Karakuri
Karakuri

Reputation: 38605

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.

Upvotes: 2

Related Questions