Home > DeveloperSection > Articles > Adapter in Android

Adapter in Android

Android Android  ListView 
0 Comment(s)
 743  View(s)
Rate this:

Adapter in Android

Adapters are the link between a set of data and the AdapterView that displays the data.


AdapterViews are ViewGroups that display child views given to it by an adapter. An example of an AdapterView is a ListView.

Adapters get the data and pass it, along with a child view, to the parent AdapterView which displays the child view and the data.

Adapters call the getView() method which returns a view for each item within the adapter view. Get a View that displays the data at the specified position in the data set.

Some useful Adapters-:

1.      ArrayAdapter

2.      CursorAdapter

3.      BaseAdapter

4.      ListAdapter

Example of ArrayAdapter-:

My listview_item xml file


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"






    android:paddingTop="5sp" >





        android:src="@drawable/ic_launcher" />









        android:text="Sample Application" />






public class MainActivity extends Activity {

     List<String> myList;

     ListView listview;



     protected void onCreate(Bundle savedInstanceState) {




           myList = new ArrayList<String>();

           for (int i = 1; i < 21; i++)

                myList.add("Item " + i);

           MyAdapter adapter = new MyAdapter(getApplicationContext(),

                     R.layout.listview_item, myList);





This is my Adapter class


private class ViewHolder {

           TextView text;

           // ImageView imgIcon;




     class MyAdapter extends ArrayAdapter<String> {

           Context myContext;

           LayoutInflater inflater;

           List<String> myList;


 public MyAdapter(Context context, int resource, List<String> objList) {

                super(context, resource, objList);

                // TODO Auto-generated constructor stub

                inflater = LayoutInflater.from(context);

                myList = objList;

                myContext = context;




public View getView(int position, View convertView, ViewGroup parent) {

                // TODO Auto-generated method stub

                final ViewHolder holder;

                if (convertView == null) {

                     holder = new ViewHolder();

         convertView = inflater.inflate(R.layout.listview_item, null);

                     // Locate the TextViews in listview_item.xml

                     holder.text = (TextView) convertView




                } else {

                     holder = (ViewHolder) convertView.getTag();




                return convertView;




Example of baseAdapter

Simply add BaseAdapter instead of ArrayAdapter<String> or ArrayAdapter. It will be add 4 override method.



class MyAdapter extends BaseAdapter {



           public int getCount() {

                // TODO Auto-generated method stub

                return 0;




           public Object getItem(int position) {

                // TODO Auto-generated method stub

                return null;




           public long getItemId(int position) {

                // TODO Auto-generated method stub

                return 0;




public View getView(int position, View convertView, ViewGroup parent) {

                // TODO Auto-generated method stub


                return null;



Difference between ArrayAdapter and Base Adapter-:

Base Adapter as the name suggests, is a base class for all the adapters. When you are extending the Base adapter class you need to implement all the methods like getcount( ), getid( ) etc.

ArrayAdapter is a class which can work with array of data. You need to override only getview method.

Base adapter is abstract class whereas array adapter and the list adapter are the concrete classes.

Common base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface} and Spinner (by implementing the specialized SpinnerAdapter interface.

A concrete BaseAdapter that is backed by an array of arbitrary objects.

Don't want to miss updates? Please click the below button!

Follow MindStick