Android ListView with Custom Adapter and ViewHolder

In this Android list view sample, it demonstrates list view using custom array adapter with view holder. ViewHolder makes the list view to load faster by caching the views. If it look up the view every time by calling the findViewById(), it will be very slow.
activity_main.xml, layout file for the main activity.

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

list_item.xml, layout file for row items in the list view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="match_parent"
</LinearLayout>, in this main activity’s onCreate method will initialize the list view adapter.

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView listView;

    protected void onCreate(Bundle savedInstanceState) {

        // Initializing list view with the custom adapter
        ArrayList <Item> itemList = new ArrayList<Item>();
        ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, itemList);
        listView = (ListView) findViewById(;

        // Populating list items
        for(int i=0; i<100; i++) {
            itemList.add(new Item("Item " + i));

        // Set up list item onclick listener

    private void setUpListItemClickListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), "item " + position + " clicked", Toast.LENGTH_SHORT).show();
}, a very simple item class for creating items in the list view.

public class Item {

    private String name;

    public Item(String n) {
        name = n;
    public String getName() {
        return name;

    public void setName(String name) { = name;
}, the custom list view adapter with viewholder pattern.

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

public class ItemArrayAdapter extends ArrayAdapter<Item> {

    private int listItemLayout;
    public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
        super(context, layoutId, itemList);
        listItemLayout = layoutId;

    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Item item = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(listItemLayout, parent, false);
            viewHolder.item = (TextView) convertView.findViewById(;
            convertView.setTag(viewHolder); // view lookup cache stored in tag
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        // Populate the data into the template view using the data object

        // Return the completed view to render on screen
        return convertView;

    // The ViewHolder, only one item for simplicity and demonstration purposes, you can put all the views inside a row of the list into this ViewHolder
    private static class ViewHolder {
        TextView item;

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search