Multi-Line ListView Entries in Android

How to Place Multiple Lines in Each Android ListView Entry

When most people think of a list they think of a single line of text per line. On an Android device a list can have one, two, three and many more lines per list entry. In the article Two Line Lists In Android it was shown how to create a two line entry for each list item. For multiple lines per list entry the same code is used, it is just extended for each additional line. Read the previous article to understand the principles involved. Here we extend that article’s code to add another, third, line to each list entry.

Define the Data

Again the data used is the name of an Amercian State and the state Capital, plus the state population from the 2010 census. The array containing this data is now defined thus:

[code lang=”Java”]private String[][] States_Capitals_Population =
{{"Alabama","Montgomery","4,779,736"},
{"Alaska","Juneau","710,231"},
{"Arizona","Phoenix","6,392,017"},
{"Arkansas","Little Rock","2,915,918"},
{"California","Sacramento","37,253,956"},
{"Colorado","Denver","5,029,196"},
{"Connecticut","Hartford","3,574,097"},
{"Delaware","Dover","897,934"},
{"Florida","Tallahassee","18,801,310"}};[/code]

Define the View for Each List Entry

The file my_two_lines.xml now becomes multi_lines.xml, with a new TextView with id line_c to hold the state population, for example: Continue reading

Two Line Lists In Android

A SimpleAdapter Loads a ListActivity with Two Line Entries

In the article Add a Simple List to an App example code was given to show how to load a ListActivity from a string array. This article shows how each list item in a ListActivity can display two lines of data, in this case each list item will be an American state and its capital. It is assumed that you know Java and that your computer is set up for Android development using Eclipse, if not see Set Up Windows for Android Development.

Start by creating a new Android Project in Eclipse using the File menu, select New then Android Project. Fill in the Project Name, here State Capitals is used. Click Next and select the build target from the installed APIs, e.g. Android 1.5, any of the installed APIs will work and it can be changed later if required. Click Next and enter the Package Name in the required format, e.g. biz.tekeye.statecapitals, leave Create Activity checked with the default Activity name, click Finish.

Overview

A common pattern in Android is to define a View in XML, set up the data that will be displayed in the View, then provide the code to load that data into the View. In the case of a ListView or ListActivity that code is provided by an Adapter. For a list with single line entries an ArrayAdapter does the job as seen in the previously mentioned article. Adding another line of data for each entry in the list requires the SimpleAdapter.

The code shown here will define a two dimensional string array to hold the American States names and capitals data. Two TextViews in a layout define the template for each list entry. The work of inflating the Views for each list entry and setting the values in the Views is done by a SimpleAdapter. An ArrayList and HashMap ensures the data goes into the correct TextView. This diagram summarises what the code achieves.

ListActivity and SimpleAdapter for Two Lines of Strings

Continue reading