Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView and OnClickListener: How to get the selected item

I have a listView vith some items.

I would like to get from my onClickListener the name (String) of the selected item.

I know how to get the selected position but how to find the String of this element?

Here is my on click listener:

journalNames.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {

    }});

My listView is populated with some query from the database.

Thank you.

like image 975
Milos Cuculovic Avatar asked Feb 24 '12 08:02

Milos Cuculovic


People also ask

How do I get the selected list of items?

To get which item was selected, there is a method of the ListView called getItemAtPosition. Add this line to your OnItemClick method: String itemValue = (String) theListView. getItemAtPosition( position );

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });


1 Answers

What about,

journalNames.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {
      String selectedFromList = (journalNames.getItemAtPosition(position).getString());
    }});
like image 168
user370305 Avatar answered Sep 20 '22 00:09

user370305