Custom list clicking with checkboxes

I’ve populated a ListActivity from a Cursor using SimpleCursorAdapter that starts another activity when one of the list items have been clicked. I’m also using ViewBinder to do some custom transformation of the data.

I want to add a CheckBox to each row in the list so I’ve changed the view and added a CheckBox with gravity right.

  • getMenuInflater( ) method undefined issue in Android Context Menu creation
  • Android 5.0 makes unselected checkboxes invisible when background is white
  • Get the height of a TextView
  • Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?
  • Parcelable protocol requires a Parcelable.Creator object called CREATOR (I do have CREATOR)
  • ADB Shell giving bad mode when executing chmod (under su)
  • Adding the CheckBox has removed the ability to click on the items. The onListItemClick method I was overriding in ListActivity is no longer called when you press on a list item. Removing the CheckBox fixes this. Why is this?

    Also, how can I set up the list so that it continues to perform my required functionality if the main part of the list item is clicked but have additional functionality when the CheckBox in the item is checked? Will setting a onCheckedChangedListener work or is the same view instance reused for each item in the list?

    Related posts:

    JNI ERROR (app bug): local reference table overflow (max=512)
    ListView - ImageLoader moves List / Items on Scroll up
    Custom sync not working with Google Account (com.google) on some Samsung devices
    ACCESS_NETWORK_STATE permisson on Android ICS
    Marshmallow Permissions not working for TargetVersion below 23
    How does the Android source overlays work?
  • @IntDef annotation and return value from other's code that cannot be annotated or how to temporarily disable annotation from affecting the code?
  • How to extend android class which implements Parcelable interface?
  • When to use a thread / service in Android?
  • How can I have both res/values-pt_PT and res/values-pt_BR on Android?
  • Android Studio missing exception stacktrace in Logcat
  • How to get TimeZone from android mobile?
  • 2 Solutions collect form web for “Custom list clicking with checkboxes”

    As explained here, the click listener only works if no other view is focusable. Setting your CheckBox to focusable="false" should do the trick:

    <CheckBox android:focusable="false" />
    

    Looks like SimpleCursorAdapter is too primitive for what I wanted to achieve.
    I’ve switched to implementing CursorAdapter and returning a new view using the LayoutInflater in my implementation of the newView method.

      public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.alarm_row, parent, false);
      }
    

    In bindView I then set a custom OnClickListener to my main LinearLayout and then another OnCheckedChangeListener to the CheckBox.

    For all this to look right I had to set the LinearLayout‘s background to android’s menuitem drawable:

    android:background="@android:drawable/menuitem_background"
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.