My app is currently targeted at Android 1.6. It contains a ListView with CHOICE_MODE_SINGLE. All the items implement Checkable. I am using setItemChecked(int position, boolean value) to check/uncheck items as needed. It works as expected on Android 2.1, 2.2 and 2.3. On Android 1.6 however, no item gets checked.
Code looks like this:
Integer checkedIndex = 0; // This is actually set from somewhere else.
void updateCheckedItem() {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
listView.setItemChecked(i, isChecked(i));
}
// Here, we should have a checked item (unless checkedIndex was null)
SparseBooleanArray checkedPositions = listView.getCheckedItemPositions();
int size = checkedPositions.size();
// On Android 1.6, size is 0 (wrong)
// On Android 2.x, size is 1 (correct)
// Another try...
int checkedPosition = listView.getCheckedItemPosition();
// On Android 1.6, checkedPosition is INVALID_POSITION (-1), meaning nothing is checked (wrong)
// On Android 2.x, checkedPosition is whatever checkedIndex is (correct)
}
boolean isChecked(int position) {
return checkedIndex != null && checkedIndex == position;
}
This question resolved the problem by setting ListView's ChoideMode in code, not XML. I did that in code to begin with and putting it in XML made no difference for me. Problem still occurs.
How can I make this work on Android 1.6?