---
title: "Adapter in Android"  
description: "Adapters are the link between a set of data and the AdapterView that displays the data."  
author: "Manoj Pandey"  
published: 2015-05-30  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1786/adapter-in-android  
category: "android"  
tags: ["android", "listview"]  
reading_time: 3 minutes  

---

# Adapter in Android

[Adapters](https://www.mindstick.com/articles/12797/power-plug-adapters-your-perfect-travelling-companions) are the [link](https://www.mindstick.com/articles/23633/link-builder) between a set of [data](https://www.mindstick.com/articles/326933/5-reasons-big-data-is-important-to-the-logistics-industry) and the AdapterView that displays the data.

##### AdapterView :

AdapterViews are ViewGroups that display child [views](https://yourviews.mindstick.com/view/87865/my-views-on-satanic-versus-most-controversial-book-on-islam) given to it by an adapter. An example of an AdapterView is a [ListView](https://www.mindstick.com/articles/12744/listview-in-android).

Adapters get the data and pass it, along with a [child view](https://www.mindstick.com/forum/33462/how-to-pass-data-from-a-child-view-to-a-parent-view), to the [parent](https://www.mindstick.com/blog/154/how-to-find-parent-of-control-in-wpf) AdapterView which displays the child view and the data.

Adapters call the getView() [method](https://www.mindstick.com/forum/166/webservice-method) which returns a view for each [item](https://www.mindstick.com/forum/156564/remove-item-in-an-array-in-javascript) within the adapter view. Get a View that displays the data at the specified position in the data set.

![Adapter in Android](https://www.mindstick.com/mindstickarticle/74c31f4b-844d-4c12-bdf0-1846c71e6ae9/images/b22ae484-6bdb-44a2-bf4f-b1bf35132a1b.png)

##### Some useful Adapters :

- ArrayAdapter
- CursorAdapter
- BaseAdapter
- 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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:paddingLeft="10sp"    android:paddingRight="10sp"    android:paddingTop="5sp" >     <ImageView        android:layout_width="40sp"        android:layout_height="40sp"        android:src="@drawable/ic_launcher" />     <TextView        android:layout_marginLeft="10sp"        android:layout_marginTop="9sp"        android:id="@+id/myTextview"        android:layout_width="wrap_content"        android:textSize="20sp"        android:layout_height="wrap_content"        android:text="Sample Application" /></LinearLayout>
```

##### MainActivity.class

```
  public class MainActivity extends Activity {     List<String> myList;     ListView listview;      @Override     protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);           listview=(ListView)findViewById(R.id.listview);           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);           listview.setAdapter(adapter);   }}
```

##### 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;      }       @Override      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 .findViewById(R.id.myTextview);                  convertView.setTag(holder);             } else {                 holder = (ViewHolder) convertView.getTag();             }             holder.text.setText(myList.get(position));              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 {       @Override      public int getCount() {           // TODO Auto-generated method stub           return 0;      }       @Override      public Object getItem(int position) {           // TODO Auto-generated method stub           return null;      }       @Override      public long getItemId(int position) {           // TODO Auto-generated method stub           return 0;      }       @Override      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.

---

Original Source: https://www.mindstick.com/articles/1786/adapter-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
