Data Adapters in android

Adapters are like magician. In android if you want to display block of data in List view or Grid view or any other android widget you have to implement some kind of adapter class in your application. It can be Base adapter or Array adapter or any. So in this class you perform some ugly or magical work. Like creating view for each row of list or grid and then assign data and even you can write some business logic also in that adapter class.

Types of adapters in android are as follows
  1. Base Adapter
  2. Simple Adapter 
  3. Array Adapter
  4. Simple Cursor Adapter
  5. List Adapter
  6. Cursor Adapter
  7. Resource Cursor Adapter
  8. Spinner Adapter
  9. Simple Cursor Tree Adapter
  10. Cursor Tree Adapter
  11. Header View List Adapter
  12. Wrapper List Adapter

Well some of them are totally new for me and i hope for you also. Because they are less known and less used by developers. They consist lots of complexity but for different uses it may help you. just check it out in android documentation. These are the list of adapters till android v 4.4 Kitkat. it may happen android team will add new adapters in next version of android named as android L or in future . So from above all the adapters which are mostly used in android and easy to implement. 

Base Adapter
As name suggest it is base class for all adapter class and interface. Base adapter is abstract class.Base Adapter is a very generic adapter that allows you to do pretty much whatever you want. However, you have to do a bit more coding yourself to get it working.using this class you can define custom view for each row and also you can perform task on object click event also. getView method is basically implemented in this class and you can override it and define your layout in this method. using this class you can bind data with list view and spinner. this is the most used adapter class for binding data in any view. even you can write business logic also in this class. in base adapter class you have to implement even getcount method because on the basis of that adapter will call getview method. Read more

Simple Adapter
when you want to bind static data set to list view you can use simple adapter. you can define xml layout for each row and data can be in array list or in map. each row represent single record of array list. simple adapter class extends Base Adapter class so you have all those methods so you can override even in simple adapter class. data binding is done using view Binder method. Read more

Array Adapter
Second is Array Adapter. Array Adapter is a class which can work with array of data. you need to override only getview method. you can override getview method with different types of parameter. but the main use of array adapter when you want to display a single text view in each row of list. then it is batter to use this adapter. you have to pass data layout or resource file id and text view id in getview method. you can even use image view and other object but for that Base adapter class is perfect. Read more

Simple Cursor Adapter
If your application requires data from local database then you can use simple cursor adapter class to bind data. this is class like simple adapter if you require to display data from database in listview then it is best practice to extend this class. it also works as simple adapter class. different between both is in simple adapter data is in array list and static and in this class data is in cursor and comes from database. data binding in this also done using view Binder method. Read more

Cursor Adapter
if you want to implement this class make sure your table consist one column named as "_id". because cursor traverse data using this column in cursor adapter. and if you want to perform some filtration or swipe cursor then only use this class. it is quite complex so if you are new in android don't try this try some other adapters. one more thing in cursor adapter you have to implement or override new view and bind view method. in which you can define view for each row and bind data also. cursor view already implement getview method so it is better to not to override this method. Read more

Simple Cursor Tree Adapter
last but not list simple cursor tree adapter is useful when you have nested view like nested list view or grid view.  the class provide you method to define layout for child view or group view so if you have data from database and even it is complex (two / three table data) then you can use this adapter class to display data in nested view. Read more

Well there are other adapters also in the list but they are less used and i just covered few details of each adapter class you can find more details about it. click on read more and it will redirect you to documentation where you can find more about it. one more thing, in android View Holder pattern is most famous and use full pattern of saving row layout before it get recycled by garbage collector. so before implementing look after this pattern.

I hope these details help you to understand about adapters. and if it is helpful to you please share this article with your friends. and if you have any thing to say about this article write down in below comment box. new things and suggestion always welcome.
Keep coding...

1 comment :