OnItemClickListener and OnClickListener not working for ListView

39,531

Solution 1

The following will do the job in your case.

ListView propertylistview = (ListView) findViewById(R.id.listview); 
    propertylistview.setOnItemClickListener(  myListViewClicked ):

        OnItemClickListener myListViewClicked = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(YourActivity.this, "Clicked at positon = " + position, Toast.LENGTH_SHORT).show();

            }
        };

Dont forget to remove the following from the CustomAdapter

  convertView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

         Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
    }
});

Solution 2

If the touch events are getting intercepted inside the cell layout, in the layout for your custom cell, set android:descendantFocusability="blocksDescendants" on the top level layout of your cell. For me, it was too much of a pain to add xml attributes to every individual view.

Note that this can also be set in code:

cell.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

I also tried this with a ListView inside a cell, and I had to override onTouchEvent to get it to work:

public class NoTouchListView extends ListView {

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

Solution 3

the simple code that solved my problem, and method view.setOnClickListener be within my custom adapter

    view.setFocusable(false)

Solution 4

Check your list row layout once :

layout or any view should not be `android:clickable="true" and android:focusable="true"

if it is there just delete and run the application again.

Solution 5

Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,

Added the following properties to the checkbox,

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

and ItemClickListner started working.

For detailed example you can go through the link,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

Hope it helps Cheers!!

Share:
39,531
Mahe
Author by

Mahe

Updated on July 05, 2022

Comments

  • Mahe
    Mahe almost 2 years

    I have used a Custom ListView and I am displaying some data using the same ListView.

    When I click on the List View item, the onClickListener is not getting called. I am not able to select any list item.

    Layout Code:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="16dp"
    android:background="@drawable/list_selector"
    android:clickable="true"
    android:orientation="horizontal"
    android:padding="5dip" >
    
    <LinearLayout
        android:id="@+id/imgProperty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >
    
        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher" 
            android:focusable="false"/>
    </LinearLayout>
    
    <TextView
        android:id="@+id/tvCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="75dip"
        android:layout_toRightOf="@+id/list_image"
        android:paddingBottom="10dip"
        android:text="property"
        android:textColor="#040404"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />
    
    <TextView
        android:id="@+id/tvprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imgProperty"
        android:layout_alignLeft="@+id/tvCity"
        android:text="Price" 
        android:focusable="false"/>
    
    </RelativeLayout>
    

    Adapter code:

     public class CustomListAdapter extends BaseAdapter {
    
     ArrayList<Propety> PropertiesArray;
    private LayoutInflater Inflater;
    
    
    public CustomListAdapter(ArrayList<Propety> PropertiesArray) {  
    
       this.PropertiesArray=PropertiesArray;
    
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return PropertiesArray.size();
    }
    
    @Override
    public Object getItem(int position) {
    
        return PropertiesArray.get(position);
    }
    
    @Override
    public long getItemId(int position) {
    
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
    
         if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                convertView = inflater.inflate(R.layout.customlistview, parent, false);
            }
    
    
            final Propety ListArray = PropertiesArray.get(position);
    
           TextView tvPropertyName = (TextView) convertView.findViewById(R.id.tvCity);
           tvPropertyName.setText(ListArray.getName());
    
            TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice);
            tvPrice.setText(ListArray.getPrice());
    
           ImageView imgProperty = (ImageView) convertView.findViewById(R.id.list_image);
           imgProperty.setImageResource(R.drawable.ic_launcher);
    
    
           convertView.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                 Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    
    
        return convertView;
    }
    
    }
    

    ListView code:

     ListView propertylistview = (ListView) findViewById(R.id.listview);
    CustomListAdapter customlistview=new CustomListAdapter(PropertiesArray);
    propertylistview.setAdapter(customlistview);
    

    ListView XML:

     <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/customview"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/list_selector"
        android:textAlignment="center" >
    

    custom.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    
    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/txtangle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="115dp"
        android:layout_marginLeft="95dp"
        android:text="" />
    
    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/customview"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/list_selector"
        android:textAlignment="center" >
    
    </ListView>
    
    <view
        android:id="@+id/customview"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        class="com.example.samplebuapp.CustomCompass" />
    
    <view
        android:id="@+id/view1"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_above="@+id/listview"
        android:layout_alignParentRight="true"
        android:layout_marginRight="18dp"
        class="com.example.samplebuapp.CustomView" />
    
    <LinearLayout
        android:id="@+id/rl1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/listview"
        android:orientation="vertical" 
        android:focusable="false">
    </LinearLayout>
    
    </RelativeLayout>
    

    Even scrolling is not working.

    I am unable to figure out why is this happening? Am I missing out something?

    Any help in resolving this is appreciated.

  • Mahe
    Mahe about 11 years
    Thanks @Ninja. Thats not working. I am displaying the ListView on a surfaceView. Displaying ListView on a SurfaceView causes any problem??
  • Duncan Jones
    Duncan Jones over 9 years
    You use myListViewClicked before you've declared it..?
  • Ken Ratanachai S.
    Ken Ratanachai S. almost 9 years
    By assigning OnItemClickListener to a variable first (then pass into setOnItemClickListener), it mysteriously solved my problem. Then after that when I do it without having to assign to variable then it still works.)