How to handle ListView click in Android
Asked Answered
P

9

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.

Prostrate answered 18/3, 2010 at 7:19 Comment(0)
F
185

On your list view, use setOnItemClickListener

Fanchie answered 18/3, 2010 at 7:23 Comment(2)
How do you then send data to that detailed view? (Like youtube clicking through to a video)Bract
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(); }Sheepish
B
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
    */  
  }
});
Black answered 22/12, 2010 at 13:1 Comment(4)
lv is not accessible inside onItemClick. What's a nice way to get around that?Brat
@Brat Make it final. final ListView lv = (ListView) findViewById(R.id.your_list_view);Ascent
Clean way: Object o = ((ListView)arg0).getItemAtPosition(position);Lura
Here is the link to the documentation: developer.android.com/reference/android/widget/…Swetlana
T
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

Tejada answered 18/3, 2010 at 7:35 Comment(5)
But this doesn't determine what item is being clicked, which makes it more-or-less useless for a list? Correct?Marroquin
you can set tag to convertView and check the same in onClick.. using tag which item is clicked can be identified...Tejada
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.Simonides
Your onClick method is wrong. It should be onItemClick(AdapterView<?> parent, View view, int position, long id)Milkfish
@Milkfish 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.Tejada
T
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.

Tomkin answered 18/3, 2010 at 8:19 Comment(0)
V
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
}
Villous answered 2/8, 2014 at 6:16 Comment(2)
good solution but, there is no way to get position of item clickedMaratha
@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.Villous
A
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();
    }
});
Anyhow answered 23/5, 2013 at 13:9 Comment(0)
S
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

Sheepish answered 5/4, 2014 at 9:52 Comment(0)
G
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.
}

});
Gamine answered 20/1, 2015 at 12:31 Comment(0)
D
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()

 }
Dexedrine answered 12/4, 2019 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.