Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle ListView click in Android

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.

like image 563
teepusink Avatar asked Mar 18 '10 07:03

teepusink


People also ask

How we can define what should happen when a list item gets clicked on?

This example demonstrates how do I handle the click event in ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can you react to click events on an item of a ListView?

One potential way to achieve is it set your ListView to height="wrap_content" If you do that then you can have override onTouchEvent() for your activity to get the events from when the user touches the empty space.

How do I set up OnItemClickListener?

OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Object listItem = list. getItemAtPosition(position); } }); In the sample code above, the listItem should contain the selected data for the textView .


4 Answers

On your list view, use setOnItemClickListener

like image 119
David Hedlund Avatar answered Oct 16 '22 13:10

David Hedlund


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
    */  
  }
});
like image 29
Aditya Mehta Avatar answered Oct 16 '22 13:10

Aditya Mehta


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

like image 42
Vijay C Avatar answered Oct 16 '22 12:10

Vijay C


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.

like image 17
Dimitar Dimitrov Avatar answered Oct 16 '22 13:10

Dimitar Dimitrov