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:

((ListView)findViewById(R.id.list)).setOnItemClickListener(
    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
        sb.append(States_Capitals_Population[position][0]+"\n");
        sb.append(States_Capitals_Population[position][1]+"\n");
        sb.append(States_Capitals_Population[position][2]+"\n");
        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");
    sb.append(v.getText()+"\n");
    v=(TextView) view.findViewById(R.id.line_b);
    sb.append(v.getText()+"\n");
    v=(TextView) view.findViewById(R.id.line_c);
    sb.append(v.getText()+"\n");
    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 =
        (HashMap<String,String>)((ListView)MainActivity.this.findViewById(R.id.list)).getItemAtPosition((int)id);
    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 LinkedInSubmit to redditPrint Page

Do you have a question or comment about this article?

(Alternatively, use the email address at the bottom of the web page.)

 This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

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


Articles on:

Android Programming and Android Practice Projects, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS, C# Programming, Using Windows for Programming


Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples



Tek Eye Published Projects