Tek Eye Logo

Tek Eye

Read the Selection From a Multi Line Android ListView

This article shows you how to read the selected data from an Android ListView where each list item has multiple lines. 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.

Android Logo

(This Android multi line ListView data tutorial assumes that Android Studio is installed, a basic App can be created and run, and the code in this article can be correctly copied into Android Studio. The example code can be changed to meet your own requirements. When entering code in Studio add import statements when prompted by pressing Alt-Enter.)

Getting the Selected Data from an Android Multi Line List

This tutorial follows on from the article Multi Line ListView Entries in Android. Use the code developed for that article for this tutorial. The file multi-line.zip can be used to import the previous project code into Android Studio. It is also available from the free Android Example Projects page.

To read the data from the ListView the setOnItemClickListener needs to be passed a reference to an OnItemClickListener. (Here an AdapterView version because the Adapter creates each View in the list.) If you are not familiar with how listeners can be set up in Android see the article Different Ways to Code Android Event Listeners. The same way for setAdapter the familiar findViewById is used:

    new AdapterView.OnItemClickListener() {...});

The click event is given the View that was passed 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:

((ListView)findViewById(R.id.list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        StringBuilder sb = new StringBuilder("Selected:\n");
        //By position in array
        Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();

Multiline List Data Access in Android

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

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");
    v=(TextView) view.findViewById(R.id.line_b);
    v=(TextView) view.findViewById(R.id.line_c);
    Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();

Another way is to use the id parameter 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.

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 =
    sb.append(selected.get("line1") + "\n");
    sb.append(selected.get("line2") + "\n");
    sb.append(selected.get("line3") + "\n");
    Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();

The example source is available from the read-multi-line.zip file ready for importing into Android Studio. The zip file is also available from the free Android Example Projects page which has instructions on importing.

See Also

Author:  Published:  Updated:  

ShareSubmit to TwitterSubmit to FacebookSubmit to Google+Submit to LinkedInSubmit to redditPrint Page

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.

Articles on:

Android, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS

Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples