Grab the Data From a Multiline Android List

Getting the Selected Data from an Android Multiline List

This article shows you how to read the selected data from an Android multiline list. It follows on from the articles Two Line Lists In Android and Multi-Line ListView Entries in Android. Here the selected data is simply shown in a Toast to the user. If you have not got a project ready to use for the following code see the previous tutorials to set up the example State Capitals project in Eclipse.

In the project we used an Activity that extends a ListActivity, this makes available some additional methods to our Activity class. The method getListView() allows us to grab the reference to the ListView which can be used to set the listener which will respond a list item being selected (if you a not familiar with how listeners can be set up in Android see the article Different Ways to Code Android Event Listeners):

[code lang=”Java”]getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

Notice how the click event is given the View that was pressed in the list and the position of the view in the adapter. In our example the adapter was loaded from a string array and hence the postion of the data in the array corresponds to the position in the adapter. Thus position can be used to index into the data array. (If you need to copy this code see Copying Code from the Articles for tips).

[code lang=”Java”]public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
StringBuilder sb = new StringBuilder("Selected:\n");
//By position in array
Toast.makeText(StateCapitalsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();

Alternatively the data can be read directly from the TextView within the selected View using the reference passed in to the listener.

[code lang=”Java”]public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
//By finding view id
TextView v=(TextView) view.findViewById(;
StringBuilder sb = new StringBuilder("Selected:\n");
v=(TextView) view.findViewById(;
Toast.makeText(StateCapitalsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();

The id parameter can also be used to access the data in the adapter for the selected item. In our case the data is stored in a HashMap, but for other implementations using custom adapters another type of object may have been created for use in the adapter, it will depend upon the particular custom implementation.

[code lang=”Java”] public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
//By using data id
StringBuilder sb = new StringBuilder("Selected:\n");
HashMap<String,String> selected = (HashMap<String,String>) StateCapitalsActivity.this.getListView().getItemAtPosition((int)id);
sb.append(selected.get("line1") + "\n");
sb.append(selected.get("line2") + "\n");
Toast.makeText(StateCapitalsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();

Multiline List Data Access in Android

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 =
{"Arkansas","Little Rock","2,915,918"},

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