1

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.

prolink007
  • 33,872
  • 24
  • 117
  • 185
Ahmed
  • 2,966
  • 7
  • 42
  • 69
  • [See this tuts](http://samir-mangroliya.blogspot.in/p/android-image-listview.html) – Samir Mangroliya Jul 16 '12 at 17:45
  • Thanks for the reply. But thats not what I am looking for. Its similar to other links that I have. Let me provide you some better ones :D http://android.amberfog.com/?p=296 http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications If you want I can provide you more :D ... But I am looking for a generic Adapter which can handle different kinds of List items. And I dont have to hard code any layout. – Ahmed Jul 16 '12 at 17:49
  • 1
    I don not think there is any generic way to do it..what you can do is add a `switch` statement before inflating you view. But it does not make any sense...Why you want a single adapter? Make a separate adapter for each type of list.. – Mohsin Naeem Jul 16 '12 at 17:52
  • I thought a bit about this when I was working intensively with listviews but I just couldn't think of a generic way in which I could inflate the items from the layout within the adapter in getView(). However, I have a feeling that if we take the part of inflating out of the adapter and into the activity that calls the adapter, it might work out. Also things would be cooler if we could put in the ViewHolder design into it. I think i'll rethink this. – Shubhayu Jul 16 '12 at 18:04
  • @MMohsinNaeem: The reason is simple. Not to create a seperate Adapter each time a new request come's in with different item layout design. If we could have just a single Adapter it would be more easier for the developers to reuse an adapter while changing just the layouts and an Items. – Ahmed Jul 16 '12 at 19:19
  • @Shubhayu: One idea that I have is to create a list of hashtable containing items of a layout and start creating items from there, however that's not an optimum solution to my problem. If there could be something different than just a usual way of recreating an adapter. – Ahmed Jul 16 '12 at 19:22
  • @MMohsinNaeem: Who said its complex to write a separate adapter? But I think its a donkey work! That's why I am looking for a way to create a single adapter that can deal with any kind of layout we provide. – Ahmed Jul 16 '12 at 19:53
  • I think you would like give a try to smart-adapters https://github.com/mrmans0n/smart-adapters . It can handle List and Recyclerview. – Ravi Kumar Mistry Jan 08 '16 at 12:32
  • It is helpful http://stackoverflow.com/a/38350061/3496570 – Zar E Ahmer Jul 13 '16 at 11:17

1 Answers1

3

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()

RaphMclee
  • 1,623
  • 13
  • 16
  • 2
    Thanks for the reply. I like the idea but I am still unsure of doing it this way. Can you provide a code sample? Thanks :) – Ahmed Jul 27 '12 at 16:59