checkboxes using SparseBooleanArray-StackOverflow
SparseArray淺析
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
SparseBooleanArray checkedPositions = ((AlertDialog) dialog).getListView().getCheckedItemPositions(); | |
String print = ""; | |
int[] array = asIntArray(checkedPositions); | |
for (int i = 0; i < array.length; i++) { | |
print = print + " " + array[i]; | |
} | |
.... | |
private static int[] asIntArray(SparseBooleanArray checkedItems) { | |
int checked = 0; | |
// compute number of items | |
for (int i = 0; i < checkedItems.size(); i++) { | |
int key = checkedItems.keyAt(i); | |
if (checkedItems.get(key)) { | |
++checked; | |
} | |
} | |
int[] array = new int[checked]; | |
//add indexes that are checked | |
for (int i = 0, j = 0; i < checkedItems.size(); i++) { | |
int key = checkedItems.keyAt(i); | |
if (checkedItems.get(key)) { | |
array[j++] = key; | |
} | |
} | |
Arrays.sort(array); | |
return array; | |
} |
沒有留言:
張貼留言