Android ListView with Adapter Example

How android ListView with adapter works is not so clear to me until I read this awesome article with code examples of Lucas Rocha: Performance Tips for Android’s ListView.

In relation to that, I was inspired again to make a post about android ListView with Adapter, but this one is much more simple than the previous post: How To Customize Android ListView?

Today I’m going to show you the code I use whenever I wanted the user to select an “item” in an AlertDialog with ListView. If you are looking for an example AlertDialog with simple ListView (without an adapter), here’s a blog post I made: Android AlertDialog with ListView.

A ListView item here contains a text (item name) and an ID (item ID), so whenever the user selects an item in the pop up, you’ll be able to get the text and ID of that selected item.
This is very useful if the items are from a database – records with IDs and names. The tags (item IDs) were set using the TextView’s setTag() method.

Video Demo

Here’s the final output of our code for today. Name and ID were fetched and show via toast.

Video Demo Permalink


Let’s Code!

We only need 6 files to run this sample code successfully.
1. activity_main.xml – our main layout. Our trigger to show the ListView is a button.

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="Show AlertDialog with ListView" />


2. – will show the main layout, a button at the center of the screen. Clicking that button will show our awesome ListView.

package com.example.listviewpopupwithadapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {

    AlertDialog alertDialogStores;
    protected void onCreate(Bundle savedInstanceState) {
        // a button to show the pop up with a list view
        View.OnClickListener handler = new View.OnClickListener(){
            public void onClick(View v) {
                switch (v.getId()) {

    public void showPopUp(){
        // add your items, this can be done programatically
        // your items can be from a database
        ObjectItem[] ObjectItemData = new ObjectItem[20];
        ObjectItemData[0] = new ObjectItem(91, "Mercury");
        ObjectItemData[1] = new ObjectItem(92, "Watson");
        ObjectItemData[2] = new ObjectItem(93, "Nissan");
        ObjectItemData[3] = new ObjectItem(94, "Puregold");
        ObjectItemData[4] = new ObjectItem(95, "SM");
        ObjectItemData[5] = new ObjectItem(96, "7 Eleven");
        ObjectItemData[6] = new ObjectItem(97, "Ministop");
        ObjectItemData[7] = new ObjectItem(98, "Fat Chicken");
        ObjectItemData[8] = new ObjectItem(99, "Master Siomai");
        ObjectItemData[9] = new ObjectItem(100, "Mang Inasal");
        ObjectItemData[10] = new ObjectItem(101, "Mercury 2");
        ObjectItemData[11] = new ObjectItem(102, "Watson 2");
        ObjectItemData[12] = new ObjectItem(103, "Nissan 2");
        ObjectItemData[13] = new ObjectItem(104, "Puregold 2");
        ObjectItemData[14] = new ObjectItem(105, "SM 2");
        ObjectItemData[15] = new ObjectItem(106, "7 Eleven 2");
        ObjectItemData[16] = new ObjectItem(107, "Ministop 2");
        ObjectItemData[17] = new ObjectItem(108, "Fat Chicken 2");
        ObjectItemData[18] = new ObjectItem(109, "Master Siomai 2");
        ObjectItemData[19] = new ObjectItem(110, "Mang Inasal 2");
        // our adapter instance
        ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, ObjectItemData);
        // create a new ListView, set the adapter and item click listener
        ListView listViewItems = new ListView(this);
        listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());
        // put the ListView in the pop up
        alertDialogStores = new AlertDialog.Builder(MainActivity.this)


3. – each item in the list is considered as an object with name and ID.

package com.example.listviewpopupwithadapter;

//another class to handle item's id and name
public class ObjectItem {
    public int itemId;
    public String itemName;
    // constructor
    public ObjectItem(int itemId, String itemName) {
        this.itemId = itemId;
        this.itemName = itemName;


4. – where our ListView performance depends.

package com.example.listviewpopupwithadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

// here's our beautiful adapter
public class ArrayAdapterItem extends ArrayAdapter<ObjectItem> {

    Context mContext;
    int layoutResourceId;
    ObjectItem data[] = null;

    public ArrayAdapterItem(Context mContext, int layoutResourceId, ObjectItem[] data) {

        super(mContext, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext; = data;

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

         * The convertView argument is essentially a "ScrapView" as described is Lucas post 
         * It will have a non-null value when ListView is asking you recycle the row layout. 
         * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
            // inflate the layout
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
        // object item based on the position
        ObjectItem objectItem = data[position];
        // get the TextView and then set the text (item name) and tag (item ID) values
        TextView textViewItem = (TextView) convertView.findViewById(;

        return convertView;

5. – answers what will happen if the user clicks an item in the list?

package com.example.listviewpopupwithadapter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

 * Here you can control what to do next when the user selects an item
public class OnItemClickListenerListViewItem implements OnItemClickListener {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Context context = view.getContext();
        TextView textViewItem = ((TextView) view.findViewById(;
        // get the clicked item name
        String listItemText = textViewItem.getText().toString();
        // get the clicked item ID
        String listItemId = textViewItem.getTag().toString();
        // just toast it
        Toast.makeText(context, "Item: " + listItemText + ", Item ID: " + listItemId, Toast.LENGTH_SHORT).show();

        ((MainActivity) context).alertDialogStores.cancel();

6. list_view_row_item.xml – the layout of each item in the list. You can modify this if you want to have an image or subtitle for each item of your ListView.

<RelativeLayout xmlns:android=""
    android:padding="10dp" >

        android:text="Item name here..."
        android:textSize="15dp" />


This is just the first part, the second part will be about the Android ListView performance optimization with the help of Android ViewHolder Design Pattern.

Please share your thoughts about this Android ListView with Adapter Example!


7 responses to “Android ListView with Adapter Example”

  1. I want to thank your for a very useful tutorial. I am a complete beginner in android programming and I had a student project to finish. The deadline was very near so your tutorial helped me very much to finish my application in time. :)

    Thank you very much

  2. I found this tutorial very helpful and managed to implement it in my app. However, I want to display listviewitems in my main activity in listview (not in alertdialog). Seems to not to work. This is my code:

    // our adapter instance

    ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, ObjectItemData);

    // create a new ListView, set the adapter and item click listener

    //ListView listViewItems = new ListView(this);

    // ********** THIS IS WHAT I WANT TO DO, NOT New ListView

    ListView listViewItems =(ListView) findViewById(;

    ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, ObjectItemData);


    This is error that I get:


    at com.example.tapos.ArrayAdapterItem.getView(

    Please help.


    • Hey @toncibuljan:disqus , thanks for telling us you found this post helpful, regarding your problem, would you post your getView() method so we can have a clue about the error..

  3. dude, im stuck on step 2.
    using android studio 0.8.0 as IDE, cannot resolve symbol ‘ObjectItem’, ‘ArrayAdapterItem’, ‘list_view_row_item’ and ‘OnItemClickListenerListViewItem’.

    what should i do?

    • Hey @gaptek, do you have any error logs? I’m not using Android Studio but it eclipse, I would do a Project > Clean…

Leave a Reply

Your email address will not be published. Required fields are marked *