Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView with OnItemClickListener

I am using a custom ListView with RatingBar and ImageButton. Here is my problem: When I click on my ListView, my OnItemClickListener is not working. Please can any one help me. Code:

ListView lv = getListView(); setContentView(lv); lv.setOnItemClickListener(new OnItemClickListener() {     @Override     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {         Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();     } }); 
like image 788
NikhilReddy Avatar asked Oct 04 '11 09:10

NikhilReddy


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.

What is AdapterView?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.


2 Answers

Though a very old question, but I am still posting an answer to it so that it may help some one. If you are using any layout inside the list view then use ...

android:descendantFocusability="blocksDescendants"     

... on the first parent layout inside the list. This works as magic the click will not be consumed by any element inside the list but will directly go to the list item.

like image 160
Shaista Naaz Avatar answered Sep 17 '22 04:09

Shaista Naaz


I have an Activity that extends ListActivity.

I tried doing something like this in onCreate:

ListView listView = getListView(); listView.setOnItemClickListener(new OnItemClickListener() {      @Override     public void onItemClick(AdapterView<?> parent, View view,             int position, long id) {          Log.i("Hello!", "Y u no see me?");      }  }); 

But that didn't work.

Instead I simply needed to override onListItemClick:

@Override protected void onListItemClick(ListView l, View v, int position, long id) {      Log.i("Hello!", "Clicked! YAY!");  } 
like image 36
poshaughnessy Avatar answered Sep 17 '22 04:09

poshaughnessy