Handle the case of Preference lists that have header views inserted at the top.
Without this change, wrong Preference is returned when you select one.
Subtract the number of headers from the index before querying the adapter.
Needed for:
Bug: 5203189
Change-Id: Iba7277789ebbd7e3e9954931b1ea06c7e34f3c15
diff --git a/core/java/android/preference/PreferenceScreen.java b/core/java/android/preference/PreferenceScreen.java
index dd9dd25..8b99459 100644
--- a/core/java/android/preference/PreferenceScreen.java
+++ b/core/java/android/preference/PreferenceScreen.java
@@ -26,6 +26,7 @@
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
+import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListAdapter;
@@ -190,9 +191,13 @@
}
public void onItemClick(AdapterView parent, View view, int position, long id) {
+ // If the list has headers, subtract them from the index.
+ if (parent instanceof ListView) {
+ position -= ((ListView) parent).getHeaderViewsCount();
+ }
Object item = getRootAdapter().getItem(position);
if (!(item instanceof Preference)) return;
-
+
final Preference preference = (Preference) item;
preference.performClick(this);
}