Question

I know there's tons of other threads around about this same topic, but none of them seem to work with my scenario and I couldn't get my listview to work with their code. Basically, I'm using a SimpleCursorAdapter to populate a listview with items from the database. Each listview row uses a custom layout which consists of a checkbox and a line of simple text. How do I detect a click on the checkbox? I know I need to use OnItemClickListener, but I don't know how to incorporate that into my code. Here's my code:

remindersCursorAdapter = new SimpleCursorAdapter(this,
                         R.xml.view_reminders_item_layout,
                         remindersCursor, new String [] { RemindersDAO.NAME },
                         new int[] { R.id.view_reminders_item_text } );

viewRemindersListView.setAdapter(remindersCursorAdapter);

R.xml.view_reminders_item_layout is the custom listview layout file. How do I capture the checkbox from this file and set a click listener to it? Thanks for all your help!

Was it helpful?

Solution

If you want to check the checkbox when a item is clicked, you can do it by setting checked status of Checkbox in onItemClick method.

  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

       // now get the checkbox view. Then set the checked status.
        CheckBox checkbox = (CheckBox) view.findViewById(R.id.check_box);
        checkBox.setChecked(!checkbox.isChecked());
  }

If you want to detect the click on only checkbox then set the focusable true in xml. // in your custom list view item. It takes the current view focus.

      <Checkbox>
          android:focusable="true"
      </Checkbox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top