ListView with OnItemClickListener

AndroidListview

Android Problem Overview


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();
    }
});

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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!");
                
}

Solution 3 - Android

Hey check this, works for me... hope it work for u too

If list item contains ImageButton

Problem: OnItemClickListener just doesn’t repond any at all!

Reason: No idea

Solution: in code, set ImageButton's focus to “false”

1: ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton);

2: button.setFocusable(false);

Solution 4 - Android

If you want to enable item click in list view use

listitem.setClickable(false);

this may seem wrong at first glance but it works!

Solution 5 - Android

if list item view contains button or checkbox or imagebutton, the onitemclicklistener and onitemlongclicklistener not working due to it has own onclick listener.

set focusable as false

holder.button.setFocusable(false);

Solution 6 - Android

  1. Check if you are using OnItemClickListener or OnClickListener (which is not supported for ListView)
    Documentation Android Developers ListView
  1. Check if you added Listener to your ListView properly. It's hooked on ListView not on ListAdapter!

    ListView.setOnItemClickListener(listener);

  2. If you need to use OnClickListener, check if you do use DialogInterface.OnClickListener or View.OnClickListener (they can be easily exchanged if not validated or if using both of them)

Solution 7 - Android

listPaired = (ListView) findViewById( R.id.listView1 );
listPairedData = new ArrayList < String >();
araPaired = new ArrayAdapter( this, android.R.layout.simple_list_item_1, listPairedData );
listPaired.setAdapter( araPaired );
listPaired.setOnItemClickListener( listPairedClickItem );

private OnItemClickListener listPairedClickItem = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView < ? > arg0, View arg1, int arg2, long arg3) {

        String info = ( (TextView) arg1 ).getText().toString();
        Toast.makeText( getBaseContext(), "Item " + info, Toast.LENGTH_LONG ).show();
    }
};

Solution 8 - Android

You can also use lambda. Lambda syntax is not supported under Java 1.7 or earlier JVMs.

listView.setOnItemClickListener((parent, view, position, id) -> {
    ...
});

Solution 9 - Android

setClickable as false to ImageButton like this

imagebutton.setClickable(false);

and then perform OnItemClickListener to listview.

Solution 10 - Android

If you define your ListView programatically:

mListView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);

Solution 11 - Android

In Java as other suggest

listitem.setClickable(false);

Or in xml:

android:clickable="false"

It works very fine

Solution 12 - Android

Is there and image in the list view that you are using> then follow the link: http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/

I think when you work out on the link that I have provided first every thing will work fine, I have tried that. If you want a refined answer please elaborate the question with code and description.

Solution 13 - Android

Just insert the line into RatingBar:

android:isIndicator="true"

In XML the ratingbar look like this.

<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dp"
    android:isIndicator="true"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" />

Solution 14 - Android

lv.setOnItemClickListener( new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView < ? > parent, View view,
                            int position, long id) {
        // TODO Auto-generated method stub

    }
} );

Solution 15 - Android

Android OnItemClickLIstener conflicts with the OnClickListener of items of row of listview in Adapter. You just have to make sure your code is well managed and properly written with standards.

Check the answer in the link given below:

https://stackoverflow.com/questions/43836384/make-list-clickable/43836958#43836958

Solution 16 - Android

Asked by many, The childs in list must not have width "match_parent" if you are looking for listview click only.

Even if you set the "Focusable" to false it wont work. Set the child's Width to wrap_content

<TextView
    android:id="@+id/itemchild"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...

Solution 17 - Android

The most simple solution and the easiest way I can provide is this. You just need to do these two steps.

1.add this in your XML file.

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:id="@+id/list_view"
        android:layout_below="@+id/info_text"
        />

  1. Add this in your MainActivity file.

        ListView listView;
        listView = findViewById(R.id.list_view);

        String[] fruits = new String[5];
        fruits[0]="hello";
        fruits[1]="hello";
        fruits[2]="hello";
        fruits[3]="hello";
        fruits[4]="hello";

        List newList = new ArrayList(Arrays.asList(fruits));
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, newList);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                Toast.makeText(DisplayPannel.this, selectedItem, Toast.LENGTH_SHORT).show();            }
        });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNikhilReddyView Question on Stackoverflow
Solution 1 - AndroidShaista NaazView Answer on Stackoverflow
Solution 2 - AndroidposhaughnessyView Answer on Stackoverflow
Solution 3 - AndroidGrv9098View Answer on Stackoverflow
Solution 4 - AndroidRahul TiwariView Answer on Stackoverflow
Solution 5 - AndroidRVGView Answer on Stackoverflow
Solution 6 - AndroidMarek SeberaView Answer on Stackoverflow
Solution 7 - AndroidtsiftisView Answer on Stackoverflow
Solution 8 - AndroidbheatcokerView Answer on Stackoverflow
Solution 9 - AndroidRamakrishnaView Answer on Stackoverflow
Solution 10 - AndroidjmaculateView Answer on Stackoverflow
Solution 11 - AndroidVladView Answer on Stackoverflow
Solution 12 - AndroidDASView Answer on Stackoverflow
Solution 13 - AndroidAbhishek MajumdarView Answer on Stackoverflow
Solution 14 - Androiduser3977776View Answer on Stackoverflow
Solution 15 - AndroidZohaib HassanView Answer on Stackoverflow
Solution 16 - AndroidHassiView Answer on Stackoverflow
Solution 17 - AndroidZiaView Answer on Stackoverflow