Restore old/buggy behavior of ListView.getCheckItemIds() for adapters without stable IDs to support legacy code.
Change-Id: I4c6a15e5a224bc5e84949ceb4df17154a2874d20
diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java
index 1a9c0df5..d4552e3 100644
--- a/core/java/android/widget/ListView.java
+++ b/core/java/android/widget/ListView.java
@@ -3480,10 +3480,41 @@
* @return A new array which contains the id of each checked item in the
* list.
*
- * @deprecated Use {@link #getCheckedItemIds()} instead.
+ * @deprecated Use {@link #getCheckedItemIds()} instead.
*/
public long[] getCheckItemIds() {
- return getCheckedItemIds();
+ // Use new behavior that correctly handles stable ID mapping.
+ if (mAdapter != null && mAdapter.hasStableIds()) {
+ return getCheckedItemIds();
+ }
+
+ // Old behavior was buggy, but would sort of work for adapters without stable IDs.
+ // Fall back to it to support legacy apps.
+ if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
+ final SparseBooleanArray states = mCheckStates;
+ final int count = states.size();
+ final long[] ids = new long[count];
+ final ListAdapter adapter = mAdapter;
+
+ int checkedCount = 0;
+ for (int i = 0; i < count; i++) {
+ if (states.valueAt(i)) {
+ ids[checkedCount++] = adapter.getItemId(states.keyAt(i));
+ }
+ }
+
+ // Trim array if needed. mCheckStates may contain false values
+ // resulting in checkedCount being smaller than count.
+ if (checkedCount == count) {
+ return ids;
+ } else {
+ final long[] result = new long[checkedCount];
+ System.arraycopy(ids, 0, result, 0, checkedCount);
+
+ return result;
+ }
+ }
+ return new long[0];
}
/**