Adapter in Android - What is it ?


Adapters in android are the one who retrieve data from a data source such as an array, database, file etc and provide it to AdapterView's such as ListView, GridView etc. AdapterView is something that displays data given by Adapters.Basically an Adapter manages the data to be displayed in a AdapterView effectively.

Adapters are made in Android to make things easy and to do our job faster. For example if we take the case of one of the AdapterView that is a ListView, ListView is something which shows a list of items, to show a ListView without Adapters we want to write extra code that do functions like creating a list of TextViews that can be Scrolled means must be inside a ScrollView,also Adapters manages the index when user Scrolls down and to identify the click event on each of the TextView's in it, also Adapter implements pagination concepts for the contents. So we are lucky that Android's Adapter will manage all of these tasks for you in the background.

How Adapter manages all these tasks? What is AdapterView?


Lets explain it easily,Lets say we have a data source let it be an array, database or whatever data source ,then the adapter takes the data and creates a View out of it, then this View is given to someone called an AdapterView. The AdapterView Displays the data neatly on the screen.Basically what an AdapterView is it Contains lot of repeated views that display data.Adapter is responsible only for taking the data or managing the data while the AdapterView is responsible for controlling how the data is being displayed.We can take data from an array, a database etc so according to that there are different types of Adapters such as ArrayAdapter, SimpleCursorAdapter, BaseAdapter etc. ArrayAdapter takes data from an array while SimpleCursorAdapter takes data from a database, while BaseAdapter is a custom adapter which can be customized to how data can be extracted, processed and how the view should be generated.Also according to the type of display there are different AdapterView's such as ListView,GridView,Gallery,Spinner respectively that can display data vertically in a list, along rows and columns, horizontally, or in a drop down menu.Note Gallery is deprecated as of Android 4.1.

How an Adapter intelligently manages the Views it produce?

 Let say in case of a ListView to have a thousand list items to be displayed, then we may have to create a thousand TextView Objects,but creating a TextView each time we scroll down will become a huge performance issue, because creating a new View is highly expensive in Android, and so thousands of them.Also it will put great pressure in garbage collector since when we scroll down the TextView's at the top which becomes out of the screen will be no longer referenced and thus picked up by the garbage collector.So here the Adapter intelligently manages this problem by recycling the Views that get out of the display area of mobile screen and reusing it to display new View,in case of a ListView as a new View at the bottom when we scroll up. All Adapters work by calling the getView() method in it,Custom adapters are made by overriding this method.The getView() method will return a view for each item for the AdapterView to display.There is a parameter called convertView in this method.we can use this parameter for recycling.I will explain making custom Adapters in later posts.Now you must have understood what an adapter is.



Share on Google Plus

About Vaishakh

I am a Java and Android developer and a science enthusiast.I Like to explain complex things in simple language so anybody even without a graduation in computer science or Electronics can understand the concepts easily.

0 comments:

Post a Comment