diff options
| author | Alan Viverette <alanv@google.com> | 2015-06-03 15:19:13 -0700 |
|---|---|---|
| committer | Alan Viverette <alanv@google.com> | 2015-06-03 15:19:13 -0700 |
| commit | abed07f6c0186e16e1c8e8aaceaf8cf961695c66 (patch) | |
| tree | df465013f577edba788a9e5ae0798c2454e714f2 /core/java/android/widget/AdapterView.java | |
| parent | f7aa9251d091d0e52e879a8a92bd38228e58efd9 (diff) | |
Synchronize selected item data on-demand after data set invalidation
Previously, the selected item data could become inconsistent between a
call to Adapter.notifyDataSetChanged()/Invalidated() and the subsequent
layout pass.
Bug: 21614294
Change-Id: I45c9c98b2f6a8d8b8fb17de2065efa505cbf3c92
Diffstat (limited to 'core/java/android/widget/AdapterView.java')
| -rw-r--r-- | core/java/android/widget/AdapterView.java | 59 |
1 files changed, 49 insertions, 10 deletions
diff --git a/core/java/android/widget/AdapterView.java b/core/java/android/widget/AdapterView.java index 696271105639..cfe02bdd12f9 100644 --- a/core/java/android/widget/AdapterView.java +++ b/core/java/android/widget/AdapterView.java @@ -551,37 +551,67 @@ public abstract class AdapterView<T extends Adapter> extends ViewGroup { } /** - * Return the position of the currently selected item within the adapter's data set + * Returns the position of the currently selected item within the adapter's + * data set, or {@link #INVALID_POSITION} if there is nothing selected. + * <p> + * <strong>Note:</strong> Prior to {@link android.os.Build.VERSION_CODES#MNC}, + * calling this method between an adapter data set change and a subsequent + * layout pass could return invalid data. * - * @return int Position (starting at 0), or {@link #INVALID_POSITION} if there is nothing selected. + * @return the selected item's position (starting at 0), or + * {@link #INVALID_POSITION} if there is nothing selected */ @ViewDebug.CapturedViewProperty public int getSelectedItemPosition() { + syncSelectedItem(); return mNextSelectedPosition; } /** - * @return The id corresponding to the currently selected item, or {@link #INVALID_ROW_ID} - * if nothing is selected. + * Returns the row ID corresponding to the currently selected item, or + * {@link #INVALID_ROW_ID} if nothing is selected. + * <p> + * <strong>Note:</strong> Prior to {@link android.os.Build.VERSION_CODES#MNC}, + * calling this method between an adapter data set change and a subsequent + * layout pass could return invalid data. + * + * @return the selected item's row ID, or {@link #INVALID_ROW_ID} if + * nothing is selected */ @ViewDebug.CapturedViewProperty public long getSelectedItemId() { + syncSelectedItem(); return mNextSelectedRowId; } /** - * @return The view corresponding to the currently selected item, or null - * if nothing is selected + * Returns the view corresponding to the currently selected item, or + * {@code null} if nothing is selected. + * <p> + * <strong>Note:</strong> Prior to {@link android.os.Build.VERSION_CODES#MNC}, + * calling this method between an adapter data set change and a subsequent + * layout pass could return inconsistent data. + * + * @return the selected item's view, or {@code null} if nothing is selected */ + @Nullable public abstract View getSelectedView(); /** - * @return The data corresponding to the currently selected item, or - * null if there is nothing selected. + * Returns the data corresponding to the currently selected item, or + * {@code null} if nothing is selected. + * <p> + * <strong>Note:</strong> Prior to {@link android.os.Build.VERSION_CODES#MNC}, + * calling this method between an adapter data set change and a subsequent + * layout pass could return inconsistent data. + * + * @return the data corresponding to the currently selected item, or + * {@code null} if there is nothing selected. */ + @Nullable public Object getSelectedItem() { - T adapter = getAdapter(); - int selection = getSelectedItemPosition(); + final T adapter = getAdapter(); + final int selection = getSelectedItemPosition(); if (adapter != null && adapter.getCount() > 0 && selection >= 0) { return adapter.getItem(selection); } else { @@ -590,6 +620,15 @@ public abstract class AdapterView<T extends Adapter> extends ViewGroup { } /** + * Synchronizes the selected item's position and ID, if necessary. + */ + void syncSelectedItem() { + if (mDataChanged) { + onLayout(false, mLeft, mTop, mRight, mBottom); + } + } + + /** * @return The number of items owned by the Adapter associated with this * AdapterView. (This is the number of data items, which may be * larger than the number of visible views.) |
