Android Program to Demonstrate an Adapter

This Android Program lets you create an Adapter in an Application using Java.

Here is source code of the Program to create an Adapter in an Application. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.


Adapters are used to bind data to View Groups that extend the AdapterView class (such as List
View or Gallery). Adapters are responsible for creating child Views that represent the underlying
data within the bound parent View.(Reto Meier)

Adapters are generally used in providing data to the ListView object. The adapter also defines how each row is the ListView is displayed.

An Adapter can be assigned to a listview via the setAdapter method call on the listview object.

Android provides some standard adapters, the most important are ArrayAdapter and SimpleCursorAdapter.


1.ArrayAdapter — The Array Adapter uses generics to bind an Adapter View to an array of
objects of the specified class. By default, the Array Adapter uses the toString value of each
object in the array to create and populate Text Views. Alternative constructors enable you to
use more complex layouts, or you can extend the class (as shown in the next section) to bind
data to more complicated layouts.

2.SimpleCursorAdapter — The Simple Cursor Adapter enables you to bind the Views
within a layout to specifi c columns contained within a Cursor (typically returned from a
Content Provider query). You specify an XML layout to infl ate and populate to display
each child, and then bind each column in the Cursor to a particular View within that
layout. The adapter will create a new View for each Cursor entry and infl ate the layout
into it, populating each View within the layout using the Cursor’s corresponding column

This class focuses on the ArrayAdapter part.

You can either use the pre-defined custom Adapter in android or u can Override/extend the ArrayAdapter class to create your own adapter.

Note: Join free Sanfoundry classes at Telegram or Youtube

While creating your own adapter u need to extend the ArrayAdapter class and override the getView method , note this is the method where all magic happens.

The getView method is used to construct, inflate, and populate the View that will be added to the parent Adapter View class (e.g., List View), which is being bound to the underlying array using this Adapter.

The getView method receives parameters that describe the position of the item to be displayed, the View being updated (or null), and the View Group into which this new View will be placed. A call to getItem will return the value stored at the specified index in the underlying array.

Now here is an implementation of Adapter in android.

Main Activity

package com.example.myadapter;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends ListActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        String str[] = new String[] { "C", "C++", "Java", "Andorid" };
        MyAdapter adapter = new MyAdapter(this, str);


package com.example.myadapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] var;
    public MyAdapter(Context context, String[] var) {
        super(context, R.layout.rowlayout, var);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.var = var;
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater li = (LayoutInflater) context
        View row = li.inflate(R.layout.rowlayout, parent, false);
        TextView text = (TextView) row.findViewById(;
        ImageView img = (ImageView) row.findViewById(;
        String s = var[position];
        if (s.startsWith("C")) {
        else if(s.startsWith("Java")){
        else if(s.startsWith("Android")){
            return row;


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:layout_height="wrap_content" >
        android:src="@drawable/ic_launcher" >
        android:textSize="80px" >



Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.