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) {
}
});[/code]

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
sb.append(StatesAndCapitals[position][0]+"\n");
sb.append(StatesAndCapitals[position][1]+"\n");
Toast.makeText(StateCapitalsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
}[/code]

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(R.id.line_a);
StringBuilder sb = new StringBuilder("Selected:\n");
sb.append(v.getText()+"\n");
v=(TextView) view.findViewById(R.id.line_b);
sb.append(v.getText()+"\n");
Toast.makeText(StateCapitalsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
}[/code]

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");
@SuppressWarnings("unchecked")
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();
}[/code]

Multiline List Data Access in Android

One thought on “Grab the Data From a Multiline Android List

  1. Pingback: Multi-Line ListView Entries in Android -Tek Eye

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.