Android

Tips on Android ListView and custom adapter

Tips on Android ListView and custom adapter

What is a ListView?

What’s the difference between a TableLayout and a ListView? Items in a TableLayout are not ‘selectable’ (unless they are buttons or text areas that support keyboard focus), however, each row of a ListView can be selected. It’s just like a Swing JList. You can attach item selection listeners to a ListView to know when the user focuses in on a particular row of a list. The biggest difference between a Swing JList and a ListView is that the model view controller separation in JList is not there in ListView. A ListView’s adapter, holds all the list’s underlying data as well as the Views necessary to render each row of the ListView. However, there are many similarities with JList, for example, when your underlying data model changes, you have to fire an event to notify the adapter’s listeners that the underlying data has changed, and the views should be refreshed. The ListView does not need to be added to a ScrollView since it automatically supports scrolling.

Be careful of the following
You have to be careful between touch mode and D-pad mode input into a ListView, read this for more information. This affects the way your ListView will respond to user input by touch or by D-Pad movement, the gist of it is use the onClickListener to respond to user input, which will work for D-Pad as well as touch input events.

You also have to be careful about setting background colors and 9patch images on a ListView, read this for more information.

Example with source code

In the following example, I will show you how to create a ListView that uses a custom adapter to display weather data. The underlying weather data is stored in a Hashtable of Hashtables. At the top level, the key is a String that represents the zipcode. The Hashtable value for this key (zipcode) contains the current temperature, humidity, and an icon that represents the weather conditions. These are all strings as well. The ListView and adapter work together to display a list of these weather conditions for various zip codes. To render this weather data, the list cell renderer uses a TableLayout to display these weather conditions – each cell is composed of a 2 row table: the first row has 2 cells – icon and temperature, the second row has 1 cell – the humidity.

The following Activity creates a ListView (with this weather data adapter) and displays it:

Let’s look at the implementation of binListViewToAdapter() next:

/** create the list data, and bind a custom adapter to the listview */
private static void bindListViewToAdapter(Activity ctx, ListView listview) {

final WeatherDataListAdapter listModelView = new WeatherDataListAdapter(ctx, listview);

Here’s the CellRendererView class, which itself is just a TableLayout container that renders the weather data Hashtable:

/** this class is responsible for rendering the data in the model, given the selection state */
private class CellRendererView extends TableLayout {

// ui stuff

mycodetips

Passionate about coding and software development holding 10+ Years of experience including 6+ years of experience on Mobile application Development.

More Posts