Java – How do I check a checkbox in OnItemClickListener()?

How do I check a checkbox in OnItemClickListener()?… here is a solution to the problem.

How do I check a checkbox in OnItemClickListener()?

I have a checkbox in my ListView row, as shown below.

===========================================
[CheckBox] [TextView] [TextView] [TextView]
===========================================

The xml code is here

<CheckBox
    android:id="@+id/course_search_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:clickable="false"
    android:focusable="false" />

And I’ve made the checkbox unclickable and focusable so that the click event will be passed to the ListView.

What I want to do here is when the user clicks the listview, check CheckBox and add the location of the listview click to an arraylist. So how do you get a CheckBox to be selected in the OnItemClickListener in the ListView?

Please help, thanks.

Solution

You can add this code in OnItemClickListener:

public void onItemClick(AdapterView parent, View view, int position, long id){
   CheckBox box = (CheckBox)view.findViewById(R.id.course_search_checkbox);
   box.setChecked(true);
}

Related Problems and Solutions