Ahmed
Ahmed

Reputation: 3260

Generic ListView Adapter

I am trying to create a Custom Adapter which can handle any kind of layout to be inflated. By generic I mean any kind of data to be set in an adapter and any kind of a layout could be used with Event Listeners to be set for an item.

For example:

I have a contact list and a country list. The Contact list has four items in its layout i.e person image, name, number and a checkBox. The Country List contains a CountryName(TextView) and a checkbox.

Is it possible for both to use the same adapter and that adapter handle all kind of items?

I wish the tick images would reset after clicking on any item.

Upvotes: 1

Views: 993

Answers (1)

RaphMclee
RaphMclee

Reputation: 1623

If you have an List with different object that can be of different kinds and need different views to display. Do it that way:

Let the object define the view by themselves. Implement an interface ViewProvider on every object. This interface should provide the method getView() which then can be called in the adapter.

The adapter has now only to get the element out of the list full of ViewProviders and call the method getView to get the view.

You will not have to worry about the recycling off the views as the views are stored in every ViewProvider and will be created only once. The update of the fields (if any) can then also be made on the Object side and not in the adapter. But you have to notify the adapter about the data change by calling notifyDataSetChanged()

Upvotes: 3

Related Questions