Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 637c0dce authored by Adam Powell's avatar Adam Powell Committed by Android (Google) Code Review
Browse files

Merge "Restore old/buggy behavior of ListView.getCheckItemIds() for adapters...

Merge "Restore old/buggy behavior of ListView.getCheckItemIds() for adapters without stable IDs to support legacy code."
parents 7a375876 463ceff8
Loading
Loading
Loading
Loading
+33 −2
Original line number Diff line number Diff line
@@ -3483,9 +3483,40 @@ public class ListView extends AbsListView {
     * @deprecated Use {@link #getCheckedItemIds()} instead.
     */
    public long[] getCheckItemIds() {
        // 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];
    }
    
    /**
     * Returns the set of checked items ids. The result is only valid if the
     * choice mode has not been set to {@link #CHOICE_MODE_NONE} and the adapter