Java Tutorials for Beginners and Professionals

Android ListView with ListAdapter Example

In the previous tutorial we learned about List View layout with a simple example. This tutorial explains list adapter which is used to create custom lists.

Android ListAdapter

List view is very simple, do you want to customize your list? ListAdapter is used to customize list view layout. It behaves like a bridge between data source and a list view. Its parent class is BaseAdapter. It is used when you want to specify layout for individual rows in the list. Don’t confuse it with ArrayAdapter, ListAdapter is an interface while ArrayAdapter is a class which work with array of data. Following figure shows a custom list view by using ListAdapter.

List Adapter

ListAdapter Constructor

Constructor of ListAdapter takes a parameter that specifies a layout resource for each row. Other than this it also has two more parameters (these two parameters are typically parallel arrays) which enable us to specify which data field to associate with which object in the row layout resource.

Methods of ListAadapter

ListAdapter has following public methods.

areAllItemsEnabled( ): It tells if all items in list adapter are enabled or not, by returning a Boolean value. If it returns true, it means all items are selectable and clickable. If it returns different values (if value changes) there is no guarantee it will take effect.

isEnabled( int position ): returns true if the item at a specific position is clickable and selectable. If position given is invalid then result can not be specified.

Example of ListAdapter

Let’s start coding with ListAdapter. In the previous tutorial of list view a simple example of computer science languages has been discussed. That was shown as a simple list. Now let’s do the same thing but in a different way. Instead of just displaying list of languages, display it with language icon.

First create a layout for each row, which has an image view and text view. Here is the code for list_single.xml