Like many others, multi-select ListView is a real problem in Android.
Instead of simply querying the selected items as a List of Object (dear Google, this is what we expect):
List selected_items = my_list_view.getSelectedItems();
we are forced to use this incredibly funny API:
SparseBooleanArray checked = my_list_view.getCheckedItemPositions(); int num_selected = 0; for(int i = 0; i < checked.size(); i++) { if(checked.valueAt(i)) { num_selected++; int key = checked.keyAt(i); boolean value = checked.get(key); if (value) {
The SparseBooleanArray named SparseBooleanArray populated with a call to the even more horribly named getCheckedItemPositions() on the ListView . But instead of returning the position of each selected / checked item in the list, it returns the position of each item in the list that has been affected, regardless of whether it is currently selected or not! Incredible, but true.
In order to calculate whether an element is really checked ONLY IN THE CURRENT, we are forced to test valueAt(i) for plausibility, while the cycle of elements is โever touchedโ.
In addition to this madness, if we want to calculate the number of selected elements, it seems to us that we are forced to increase our own counter (for example, num_selected ).
With APIs like this, few people think about developers - this is anger!
rmirabelle Jan 26 '16 at 17:15 2016-01-26 17:15
source share