Reputation: 30883
I have a ListActivty
with listview choice mode set to CHOICE_MODE_SINGLE
The item layout contains only a CheckedTextView
. By default when the activity is shown all items are unchecked. If the user clicks on an item it is automatically getting checked and check-mark is shown. Clicking another item unchecks the previous item. All this is working out of the box.
What I want to do is to uncheck an item of the user clicks on an already checked item. I have overridden onListItemClick
method to detect if the item is already checked and uncheck it like this:
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view;
if (checkedTextView.isChecked()) {
listView.setItemChecked(position, false);
}
}
The problem is that on Android 4.4 checkedTextView.isChecked()
always returns true: if I click on an unchecked item it returns true and clicking on checked item returns true as well. I have tried listview.isItemChecked(position)
but it works the same way.
Is there any other way to detect that I have clicked on a checked item so that I can uncheck it?
Upvotes: 1
Views: 2900
Reputation: 5440
To check or uncheck the item on checkbox click, you can set listener on CheckBox inside adapter. Inside onCheckedChangeListener you can do your stuff.
If you want to check and uncheck the item on list row click, you can set onitemclicklistener on the list and inside onitemclick you can write this:
String isChecked = fullItemList.get(position).getIsChecked();
CheckBox checkbox = (CheckBox) view.findViewById(R.id.check_box);
if(isChecked.equalsIgnoreCase("true")) {
fullItemList.get(position).setIsChecked("false");
checkbox.setChecked(false);
} else {
fullItemList.get(position).setIsChecked("true");
checkbox.setChecked(true);
checkedItemList.add(fullItemList.get(position));
}
Hope it helps you.
Upvotes: 2
Reputation: 1888
Well there isn't an "elegant" solution for this...
myList.setOnItemClickListener(new OnItemClickListener() {
private int itemSelectedIndex;
/**
* Initializer "proxy".
*
* @param index
* @return
*/
public OnItemClickListener init(int index) {
itemSelectedIndex = index;
return this;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (itemSelectedIndex != position) {
itemSelectedIndex = position;
getArguments().putInt(ListView.class.getName(), position);
} else {
itemSelectedIndex = -1;
listTravelBags.setItemChecked(position, false);
getArguments().putInt(ListView.class.getName(), -1);
}
}
}.init(getArguments().getInt(ListView.class.getName(), -1) /* get last checked item after config changes/restarts, etc. */));
Note: getArguments()
is a method inside a Fragment. If you want to use an Activity (or in your case a ListActivity), use SharedPreferences or your savestate Bundle.
Upvotes: 0
Reputation: 5330
KitKat AbsListView
is notifying the listener after handling the click, thus it is already checked when you call checkedTextView.isChecked()
in your listener.
Try creating your custom ListView which overrides performItemClick()
, save the item state before the super call and act upon it aftewards like this (untested):
@Override
public boolean performItemClick(View view, int position, long id) {
boolean checkedBeforeClick = isItemChecked(position);
super.performItemClick(view, position, id);
if (checkedBeforeClick) {
setItemChecked(position, false);
}
}
Upvotes: 4