An Insight On Android Adapter
Android’s Adapter is described in the API documentation as “a bridge between an AdapterView and the underlying data for that view” . An AdapterView is a group of widgets (aka view) components in Android that include the ListView, Spinner, and GridView. In general, these are the widgets that provide the selecting capability in the user interface . What is not mentioned in the documentation is that the AdapterView also provides the layout of the underlying data for the view. The Adapter really brings together the data and the layout (potentially in a complex collection of views) for each of the rows that make up the AdapterView.

Why Android Adapter?
Without Android Adapter, to implement the ListView functionality, you will need to:
- Create a TextView within a ScrollView group.
- Then you will have to implement pagination concept for the contents of the TextView.
- You will also have to write additional code to identify the click event on a particular row in the TextView.
Let us now understand the internal working of an Android Adapter and how it acts as a data pump to the adapter view
At a minimum, you will need to implement four methods. These four methods are called by Android to build your AdapterView and to return the correct information when one of the items in the AdapterView is selected.
getCount( ): indicates to Android how many items (or rows) are in the data set that will be presented in the AdapterView.getItem(int pos): get the data item associated with the item (or row) from the AdapterView passed as a parameter to the method. This method will be used by Android to fetch the appropriate data to build the item/row in the AdapterView.getItemId(int pos): This method returns the data set’s id for a item/row position of the AdapterView. Typically, the data set id matches the AdapterView rows so this method just returns the same value.getView(int position, View convertView, ViewGroup parent)Adapters call the getView() method which returns a view for each item within the adapter view. The layout format and the corresponding data for an item within the adapter view is set in the getView() method. Now, it will be a performance nightmare if getView() returns a new View every time it is called. Creating a new view is very expensive in Android as you will need to loop through the view hierarchy (using the find ViewbyID () method) and then inflate the view to finally display it on the screen.It also puts a lot of pressure on the garbage collector. That is because when the user is scrolling through the list, if a new view is created; the old view (since it is not recycled) is not referenced and becomes a candidate to be picked up by the garbage collector. So what Android does is that it recycles the views and reuses the view that goes out of focus.
Below is a visual representation of this recycle process

In the above figure, let us assume we are displaying the months in a year in a ListView. To begin with, the months January till May are shown in the screen. When you scroll the view, the month January goes out of the display area of the mobile screen. As soon as the January view goes out of the screen, the Adapter View (ListView in this case) sends the view to something called a recycler.So when you scroll up, the getView () method is called to get the next view (which is June). This method getView() has a parameter called convertview which points to the unused view in the recycler. Through the convertview, the Adapter tries to get hold of the unused view and reuse it to display the new view (which is June in this case). So, when you are creating a custom Adapter View, you should code your getView () as mentioned below:
@Override
publicView getView(intitempos, View convertView, ViewGroup parent) {
//Check if the convertview is null, if it is null it probably means that this //is the first time the view has been displayed
if (convertView == null)
{
convertView = View.inflate (context,R.layout.list_content_layout, null);
}
//If it is not null, you can just reuse it from the recycler
TextView txtcontent = (TextView) convertView.findViewById(R.id.textView1);
ImageView imgcontent = (ImageView) convertView.findViewById(R.id.imageView1);
Paintings paintingcontent = content [itempos];
txtcontent.setText (paintingcontent.imagetitle);
imgcontent.setImageResource(paintingcontent.drawableresid);
// return the view for a single item in the listview
returnconvertView;
SO MANY ADAPTERS TO CHOOSE FROM
The Adapter is actually an interface. When you dig deeper into the API, you realize there are quite a few Adapter interfaces, abstract classes and implementing classes.

All Adapters ultimately implement the Adapter interface. However, the two sub-interfaces of Adapter are ListAdapter and SpinnerAdapter. These adapter interfaces define the adapter for ListViews and SpinnerViews (see diagram below) – the two main types of AdapterViews. The source of much confusion comes from the fact that the BaseAdapter is the abstract class that implements the interfaces – all of them! In implements ListAdapter, SpinnerAdapter and Adapter! Java does not allow for multiple inheritance among classes, but a class can implement multiple interfaces which is exactly what BaseAdapter does.
Base Adapter Example
public class CourseAdapter extends BaseAdapter {
List<Course> courses = // code to get the courses ArrayList
@Override
public int getCount() {
return courses.size();
}
@Override
public Object getItem(int position) {
return courses.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
Context context = parent.getContext();
LinearLayout view = new LinearLayout(context);
view.setOrientation(LinearLayout.HORIZONTAL);
view.addView(new CheckBox(context));
TextView nameTextView = new TextView(context);
nameTextView.setText(courses.get(position).getName());
nameTextView.setPadding(0, 0, 10, 0);
view.addView(nameTextView);
TextView parTextView = new TextView(context);
parTextView.setText(Integer.toString(courses.get(position).getPar()));
view.addView(parTextView);
return view;
}
return convertView;
}
}
If you look again at the Adapter hierarchy, you’ll see lots of other adapters.So what is their purpose? When do you use the BaseAdapter and when do you use one of these other adapters? In general, these other adapters provide even more convenience, but may be constraining depending on your view and data display needs.The ArrayAdapter tries to make even the BaseAdapter simpler. Out of the box, the ArrayAdapter’s data set is always an array (or ArrayList) of objects. Each of the objects in the array is presented by a single TextView widget, by default. The text of the TextView displays the toString() of each element in the array. If these constraints work for you and your user interface, then Using the ArrayAdapter is extremely simple. You don’t have to override any methods since the class provides everything built in. You simply create an instance of the ArrayAdapter and give it to the AdapterView.
All rights reserved