141

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.

Ruan_Lopes
  • 1,381
  • 13
  • 18
teepusink
  • 27,444
  • 37
  • 107
  • 147

9 Answers9

185

On your list view, use setOnItemClickListener

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • How do you then send data to that detailed view? (Like youtube clicking through to a video) – Sauron Feb 04 '14 at 03:18
  • 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(); } – bourax webmaster Apr 05 '14 at 09:51
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
    */  
  }
});
user229044
  • 232,980
  • 40
  • 330
  • 338
Aditya Mehta
  • 911
  • 6
  • 2
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

Vijay C
  • 4,739
  • 4
  • 41
  • 46
  • But this doesn't determine what item is being clicked, which makes it more-or-less useless for a list? Correct? – farm ostrich May 20 '11 at 23:55
  • you can set tag to convertView and check the same in onClick.. using tag which item is clicked can be identified... – Vijay C Sep 02 '11 at 12:08
  • 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. – jocelyn Dec 29 '12 at 19:45
  • Your onClick method is wrong. It should be onItemClick(AdapterView> parent, View view, int position, long id) – IgorGanapolsky Oct 21 '13 at 21:14
  • @IgorGanapolsky Its not wrong..!! it was another way to handle click event. though now its not recommended. [Android Docs] (http://developer.android.com/reference/android/view/View.OnClickListener.html). And what you are saying is already there in Aditya Mehta's answer. – Vijay C Oct 22 '13 at 01:06
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.

Dimitar Dimitrov
  • 16,032
  • 5
  • 53
  • 55
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
}
Amio.io
  • 20,677
  • 15
  • 82
  • 117
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();
    }
});
António Almeida
  • 9,620
  • 8
  • 59
  • 66
Shudy
  • 7,806
  • 19
  • 63
  • 98
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

bourax webmaster
  • 748
  • 7
  • 18
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.
}

});
Amey Haldankar
  • 2,223
  • 1
  • 24
  • 22
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()

 }
Allen
  • 2,979
  • 1
  • 29
  • 34