WISHY
WISHY

Reputation: 11999

Listview onitemclicklistener not working for Check box?

I have a list view whose element is as below containing one checkbox

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:minHeight="64dp"
    android:layout_marginRight="8dp"
    android:layout_marginLeft="8dp"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageViewVoucherIcon"
        android:layout_width="50dp"
        android:layout_centerVertical="true"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textViewVoucherText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_toLeftOf="@+id/checkboxVoucherClick"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:textColor="@color/text"
        android:text="sas"
        android:layout_toRightOf="@+id/imageViewVoucherIcon" />

    <CheckBox
        android:id="@+id/checkboxVoucherClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/seperator" />
</LinearLayout>

This is my onitem click listener

 lvVoucher.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)  {
           CheckBox cb=(CheckBox)view.findViewById(R.id.checkboxVoucherClick);
            if(cb.isChecked())
            {
                Log.e("check","true");
            }else{
                Log.e("check","false");
            }
        }
    });

But it does not returns anything.

Upvotes: 0

Views: 272

Answers (4)

Sukhwant Singh Grewal
Sukhwant Singh Grewal

Reputation: 495

do work in getView

 holder.checkbox.setOnClickListener( new View.OnClickListener() {  
 public void onClick(View v) {  
  CheckBox cb = (CheckBox) v ;  
  Toast.makeText(getApplicationContext(),
   "Clicked on Checkbox: " + cb.getText() +
   " is " + cb.isChecked(), 
   Toast.LENGTH_LONG).show();
 }  
});  

Upvotes: 0

Ravi Bhandari
Ravi Bhandari

Reputation: 4698

You have to add a new line to your xml parent layout -

android:descendantFocusability="blocksDescendants"

So your updated view will be like below -

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="wrap_content">

it will sure work for you.

Upvotes: 0

manDroid
manDroid

Reputation: 1135

I had the same issue this worked for me;

Set these following properties for your checkbox:

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

Upvotes: 1

Rathan Kumar
Rathan Kumar

Reputation: 2577

remove the focus to the checkbox in layout then it will work.

like this:

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:focusable="false"
        android:focusableInTouchMode="false"/>

or else you have to write that listener in adapter itself.

Upvotes: 2

Related Questions