Onitemclicklistener Was Not Work with the Checkbox

OnItemClickListener was not work with the checkbox?

The onItemClickListener for the listView will not work if you have any clickables like buttons, ImageButton, Checkbox, etc in the listView. Add

mainListView.setItemsCanFocus(true);

Refer ListView OnItemClickListener Not Responding?

OnItemClickListener does not work with CheckBoxes

Try below code:

final CheckBox foodCheckBox = (CheckBox)findViewById(R.id.item_food_check_box);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();

foodCheckBox.setChecked(prefs.getBoolean("checked",false));

foodCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheckedValue = isChecked;
editor.putBoolean("checked", isChecked);
editor.apply();
}
});

You can use value in isCheckedValue to send anywhere you need.

And then if you want to send this value to any of the activity you can use below code

 Intent in = new Intent(MainActivity.this, IntendedActivity.class);
in.putExtra("yourBoolName", isCheckedValue );
startActivity(in);

Hope that helps.

Listview onitemclicklistener not working for Check box?

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.

CheckBox and setOnItemClickListener not working in android

You can get the instance of CheckBox inside onItemClick() by using setTag() and getTag(). You can setTag the CheckBox instance inside your getView() method as

convertView.setTag(R.id.check, viewHolder.checkbox);

And get the instance inside onItemClick() using,

CheckBox checkbox = (CheckBox) v.getTag(R.id.check);

If you have any further query you can check my blog post.

setOnItemClickListener will not work with the checkbox?

To get the instance of Checkbox inside onItemClick() you need to use setTag() and getTag() for checkbox instance. You can check my example on my blog that shows how we can get the instance of CheckBox inside onItemClick().

After getting your CheckBox instance inside onItemClick() update your content of list and notify your adapter inside onItemClick() as,

if(checkbox.isChecked())
list.get(position).setSelected(false);
else
list.get(position).setSelected(true);

adapter.notifyDataSetChanged();

Android - Why click the CheckBox does not trigger the ListView's onItemClick?

I found the solution.

Just add this two properties:

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

How to make checkbox checked in the OnItemClickListener()?

You could add this code within your 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 Topics



Leave a reply



Submit