1

I'm creating an Android app with a ListView and I'm using this line:

SparseBooleanArray checkedPositions = list.getCheckedItemPositions();

Then I want to iterate over the array but only if there is at least a single value which is true in the checkedPositions array.

Can something like this be done?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • Is this helpful http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemCount() ? – TDG Jul 05 '15 at 14:18

3 Answers3

3

You cannot do that. But what you could do is to create another method which does that for you.

public boolean containsTrueValue(SparseBooleanArray sparseBooleanArray) {
    boolean containsBoolean = false;
    for (int i = 0; i < sparseBooleanArray.size(); i++) {
        if (sparseBooleanArray.valueAt(i) == true) {
            containsBoolean = true;
            break;
        }
    }
    return containsBoolean;
}
barunsthakur
  • 1,196
  • 1
  • 7
  • 18
2

ListView has a getCheckedItemCount(), so rather than checking your SparseBooleanArray you could work it out from your ListView. So, you could check:

list.getCheckedItemCount() > 0
HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
0

ArrayList class in Java have method contains. You can cast your list to arrayList and use this method. Example:

ArrayList<Boolean> array = new ArrayList();

        array.add(false);
        array.add(false);
        array.add(false);
        array.add(true);
        array.add(false);
        array.add(false);

        boolean contains = array.contains(true);

contains variable will be true if you execute this code.

Sebastian Pakieła
  • 2,970
  • 1
  • 16
  • 24