The concept of adapter is similar to the physical adapters used in our daily life in our electrical devices etc.The general idea of an adapter in software development is identical to the one in the physical world. If you have been to different countries, you probably recognized that a lot of them are using differently shaped power sockets. Quite often, they are shaped in a way that the plug of your electrical device doesn’t fit. So, how do you connect the charger of your mobile phone or laptop to these power sockets?
The answer is simple. You get an adapter which you can put into the power socket and then you put your plug into the other end of the adapter. The adapter changes the form of your plug so that you can use it with the power socket. In that example and in most other situations, the adapter doesn’t provide any additional functionality. It just enables you to connect your plug to the power socket.
The Adapter Pattern applies the same idea to object-oriented programming by introducing an additional adapter class between an interface and an existing. The adapter class implements the expected interface and keeps a reference to an object of the class you want to reuse. The methods defined by the interface call one or more methods on the referenced object and return a value of the expected type. By doing that, the adapter class fulfills the expected contract by implementing the interface and enables you to reuse existing, incompatible implementations.
Similarly the adapter perform the same function as in above example.
Adapter gets the data from the data source and converts them to the view items which then be given to the UI Components like ListView and GridView etc for displaying.
In android, Adapter is base interface which is declared as public interface Adapter and all other sub classes like
- ArrayAdapter<T>,
- BaseAdapter,
- CursorAdapter,
- HeaderViewListAdapter,
- ListAdapter,
- ResourceCursorAdapter,
- SimpleAdapter,
- SimpleCursorAdapter,
- SpinnerAdapter,
- ThemedSpinnerAdapter,
- WrapperListAdapter
Adapter is actually a main interface.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. 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.Which one to use and under what circumstances? I think the depth and width of the Adapter hierarch tends to confuse many beginning Android developers and my hope is that this post can help clarify the choice for developers.
The Interfaces
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. So, in general, when you are using a ListView (to include GridViews) in your user interface, you are going to need an Adapter that implements (directly or indirectly) the ListAdapter. When you are using a Spinner, you are going to need an Adapter that implements (directly or indirectly) the SpinnerAdapter. Pretty simple and straightforward so far.Adapter interface defines 10 methods that must be implemented by the developer. BaseAdapter is provided as a convenience to Android developers. Rather than creating a class the implements Adapter, SpinnerAdapter or ListAdapter, just extend BaseAdapter and implement the specialized methods for either a ListView or Spinner. What’s more, the BaseAdapter already provides common implementations for many of the interfaces so you don’t have to override these unless there is a specific need.
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): This method creates the View (which may be a single View component like a TextView or a complex set of widgets in a layout) that displays the data for the specified (by position) item/row in the AdapterView.
Beyond the position parameter, the getView( ) method gets passed two other important parameters. The convertView parameter allows a previously created View to be reused. When the AdapterView is first created, this argument will be null. On subsequent calls to build/display the AdapterView, the View component used to display the item/row is supplied back to this method so that you can either reuse it or discard it depending on what needs to be displayed. The last parameter to the getView( ) method is the parent View (usually the AdapterView) to which the View for the item/row is attached.
The BaseAdapter
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 Adatper! Java does not allow for multiple inheritance among classes, but a class can implement multiple interfaces which is exactly what BaseAdapter does.In General and we wrap up the discussion for BaseAdapter as
BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc. Whenever we need a customized list in a ListView or customized grids in a GridView we create our own adapter and extend base adapter in that. Base Adapter can be extended to create a custom Adapter for displaying a custom list item. ArrayAdapter is also an implementation of BaseAdapter.There are numbers of adapter provided by android for handling different kind of situations.
- BaseAdapter – It is parent adapter for all other adapters
- ArrayAdapter – It is used whenever we have a list of single items which is backed by an array
- Custom ArrayAdapter – It is used whenever we need to display a custom list
- SimpleAdapter – It is an easy adapter to map static data to views defined in your XML file
- Custom SimpleAdapter – It is used whenever we need to display a customized list and needed to access the child items of the list or grid
Now we will discuss one by one them in breif.
1. BaseAdapter.
BaseAdapter is a parent abstract interface which implements Adapter. ListAdapter and GridAdapter,SpinnerAdapter and ArrayAdapter,CustomAdatper classes implements and extends the BasAdapter Interface. BaseAdapter Interface makes the life of the Android Developer much easier for implementing important UI Widgets like Spinner, GridView and ListView etc in a quite easy way and make the Android App more useful and good looking.CustomerAdapter overrides the four important methods of BaseAdapter as shown in below examples
which makes the things cool for and Android Programmer. As shown in Below code Snippet.
public class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
2. ArrayAdapter
An ArrayAdapter is an adapter backed by an array of objects. It links the array to the Adapter View.The default ArrayAdapter converts an array item into a String object putting it into a TextView. The text view is then displayed in the AdapterView (a ListView for example).
When you create the adapter, you need to supply the layout for displaying each array string. You can define your own or use one of Android’s, such as:
android.R.layout.simple_list_item_1
There are alternative constructors that you can use for more complex layouts. You can also display images instead of strings.
3. SimpleCursorAdapter
The SimpleCursorAdapter links the data contained in a Cursor to an Adapter View.Cursors
A cursor is a set of data. You usually get a cursor when you do a database query. The result of your query is contained in the cursor.
The SimpleCursorAdapter binds the Cursor data to an Adapter View. You define a layout that controls how each row of data is displayed.
Each row’s view is populated using the column values of the corresponding row in the cursor. The SimpleCursorAdapter gets the data out of the Cursor, puts each row of data in a layout that you define and then displays it in the Adapter View
When you construct the SimpleCursorAdapter, you specify which column’s data is to be retrieved from the cursor. You also specify which fields in your layout are to display this data. The adapter then creates a new view for each cursor entry and populates it with the corresponding cursor column values.
4. SimpleAdapter In Android
In Android SimpleAdapter is an easy Adapter to map static data to views defined in an XML file(layout). In Android we can specify the data backing to a list as an ArrayList of Maps(i.e. hashmap or other). Each entry in a ArrayList is corresponding to one row of a list.The Map contains the data for each row. Here we also specify an XML file(custom list items file) that defines the views which is used to display the row, and a mapping from keys in the Map to specific views.
Whenever we have to create a custom list we need to implement custom adapter. As we discuss earlier ArrayAdapter is used when we have a list of single item’s backed by an Array. So if we need more customization in a ListView or a GridView we need to implement simple adapter.
5. Custom SimpleAdapter In Android
Whenever we have to create a custom list we need to implement custom adapter. As we discuss earlier ArrayAdapter is used when we have a list of single item’s backed by an Array. So if we need customization in a ListView or a GridView we need to implement simple Adapter but when we need more customization in list or grid items where we have many view’s in a list item and then we have to perform any event like click or any other event to a particular view then we need to implement a custom adapter who fulfills our requirement’s and quite easy to be implemented.BaseAdapter is the parent adapter for all other adapters so if we extends a SimpleAdapter then we can also override the base adapter’s function in that class.
Important Note: We can’t perform events like click and other event on child item of a list or grid but if we have some requirements to do that then we can create our own custom adapter and extends the simple adapter in that.
6. CursorAdapter
A CursorAdapter links a Cursor’s data to a List View. You must include the database’s _id column as it’s used in processing the list item’s selection.The SimpleCursorAdapter is a subclass of CursorAdapter.
The SimpleCursorAdapter is easier to use while the CursorAdapter requires more work but allows more customization
7. ListAdapter
The ListAdapter links the data and a ListView displaying the data. The List View can display any data type provided it’s wrapped in a ListAdapter. ListAdapter provides the data source for ListView, GridViews.8. SpinnerAdapter
The SpinnerAdapter links the data to a Spinner. Spinner is a drop down list and (also called as combo box etc other languages)The data can come from any source but must be provided by a SpinnerAdapter (for example an ArrayAdapter or a CursorAdapter).
Note that the BaseAdapter implements SpinnerAdapter. Both the CursorAdapter and the ArrayAdapter are subclasses of BaseAdpater so you can also use any of these to load your Spinner.
We will discuss the following examples of Adapter in our next posts which inculde.
- What is ListView and how it is implement ?
- What is GridView and How it is implement ?
- What is Spinner and How it si implemented ?