OSDN Git Service

Fix handling of ListPreference.setValue()
authorAlan Viverette <alanv@google.com>
Tue, 23 Jul 2013 21:43:37 +0000 (14:43 -0700)
committerAlan Viverette <alanv@google.com>
Tue, 23 Jul 2013 21:43:37 +0000 (14:43 -0700)
Previously, setValue() was not calling notifyChanged(). This
prevented the summary from updating correctly. Now it calls
notifyChanged() the first time it's called and when the value
actually changes.

BUG: 9987962
Change-Id: I02dd4be6bde2969f39d30921a62a7ba908128e0e

core/java/android/preference/ListPreference.java

index f44cbe4..9edf112 100644 (file)
 
 package android.preference;
 
-
 import android.app.AlertDialog.Builder;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.res.TypedArray;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.text.TextUtils;
 import android.util.AttributeSet;
 
 /**
@@ -41,6 +41,7 @@ public class ListPreference extends DialogPreference {
     private String mValue;
     private String mSummary;
     private int mClickedDialogEntryIndex;
+    private boolean mValueSet;
     
     public ListPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -130,9 +131,16 @@ public class ListPreference extends DialogPreference {
      * @param value The value to set for the key.
      */
     public void setValue(String value) {
-        mValue = value;
-        
-        persistString(value);
+        // Always persist/notify the first time.
+        final boolean changed = !TextUtils.equals(mValue, value);
+        if (changed || !mValueSet) {
+            mValue = value;
+            mValueSet = true;
+            persistString(value);
+            if (changed) {
+                notifyChanged();
+            }
+        }
     }
 
     /**