142

How do I listen to click event on a ListView?

This is what I have now

ListView list = (ListView)findViewById(R.id.ListView01);  
...  
list.setAdapter(adapter);  

When I do the following

list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
   public void onItemSelected(AdapterView parentView, View childView, 
                                                         int position, long id) 
   {  
       setDetail(position);  
   }

   public void onNothingSelected(AdapterView parentView) {  

   }  
});  

That doesn't seem to do anything on click.
And all those code live within a class that extends Activity.

9 Answers 9

185

On your list view, use setOnItemClickListener

Sign up to request clarification or add additional context in comments.

2 Comments

How do you then send data to that detailed view? (Like youtube clicking through to a video)
First, the class must implements the click listenener : implements OnItemClickListener Then set a listener to the ListView yourList.setOnItemclickListener(this); And finally, create the clic method: @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show(); }
91

Suppose ListView object is lv, do the following-

lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

    Object o = lv.getItemAtPosition(position);
    /* write you handling code like...
    String st = "sdcard/";
    File f = new File(st+o.toString());
    // do whatever u want to do with 'f' File object
    */  
  }
});

4 Comments

lv is not accessible inside onItemClick. What's a nice way to get around that?
@shinjin Make it final. final ListView lv = (ListView) findViewById(R.id.your_list_view);
Clean way: Object o = ((ListView)arg0).getItemAtPosition(position);
Here is the link to the documentation: developer.android.com/reference/android/widget/…
42

You need to set the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.

convertView = mInflater.inflate(R.layout.list_item_text, null);
convertView.setClickable(true);
convertView.setOnClickListener(myClickListener);

and declare the click listener in your ListActivity as follows,

public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
                 //code to be written to handle the click event
    }
};

This holds true only when you are customizing the Adapter by extending BaseAdapter.

Refer the ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/List14.java for more details

5 Comments

But this doesn't determine what item is being clicked, which makes it more-or-less useless for a list? Correct?
you can set tag to convertView and check the same in onClick.. using tag which item is clicked can be identified...
I have found a more elegant solution than a tag in your adapter store a setOnClickListener reference and call it on the onClick(View v) method. then the activity can recieve the event with the position.
Your onClick method is wrong. It should be onItemClick(AdapterView<?> parent, View view, int position, long id)
@IgorGanapolsky Its not wrong..!! it was another way to handle click event. though now its not recommended. [Android Docs] (developer.android.com/reference/android/view/…). And what you are saying is already there in Aditya Mehta's answer.
17

The two answers before mine are correct - you can use OnItemClickListener.

It's good to note that the difference between OnItemClickListener and OnItemSelectedListener, while sounding subtle, is in fact significant, as item selection and focus are related with the touch mode of your AdapterView.

By default, in touch mode, there is no selection and focus. You can take a look here for further info on the subject.

Comments

8

This solution is really minimalistic and doesn't mess up your code.

In your list_item.xml (NOT listView!) assign the attribute android:onClick like this:

<RelativeLayout android:onClick="onClickDoSomething">

and then in your activity call this method:

public void onClickDoSomething(View view) {
   // the view is the line you have clicked on
}

2 Comments

good solution but, there is no way to get position of item clicked
@BilalŞimşek For a long time I've been out of Android but if the view can have some metadata, it could be the way how to get the position.
7

You have to use setOnItemClickListener someone said.
The code should be like this:

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // When clicked, show a toast with the TextView text or do whatever you need.
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    }
});

Comments

5

First, the class must implements the click listenener :

implements OnItemClickListener

Then set a listener to the ListView

yourList.setOnItemclickListener(this);

And finally, create the clic method:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show();
}

you can take a look and download code here

Comments

3

Use setOnItemClickListener() api in your activity. Following is the sample.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@Override
public void onItemClick(AdapterView<> parent, View view, int position, long id)
{
     // your code here.
}

});

Comments

1

In Kotlin, add a listener to your listView as simple as java

your_listview.setOnItemClickListener { parent, view, position, id ->   

    Toast.makeText(this, position, Toast.LENGTH_SHORT).show()

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.