Skip to content Skip to sidebar Skip to footer

Listpreference's Summary Text Is Not Updated Automatically Whenever There Is Change In Selection

Although it is being documented, by using '%s', I can display selected value as summary, it doesn't work as expected. http://developer.android.com/reference/android/preference/List

Solution 1:

This is a bug that existed on platforms prior to KITKAT (i.e. android-4.4_r0.7), and it is fixed by commit 94c02a1

The solution from @ilomambo works, but may not be the best one. I read through the source of ListPreference and came up with this solution:

File: ListPreferenceCompat.java

package com.example.yourapplication;

import android.content.Context;
import android.os.Build;
import android.preference.ListPreference;
import android.text.TextUtils;
import android.util.AttributeSet;

publicclassListPreferenceCompatextendsListPreference {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)publicListPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)publicListPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    publicListPreferenceCompat(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    publicListPreferenceCompat(Context context) {
        super(context);
    }

    // NOTE:// The framework forgot to call notifyChanged() in setValue() on previous versions of android.// This bug has been fixed in android-4.4_r0.7.// Commit: platform/frameworks/base/+/94c02a1a1a6d7e6900e5a459e9cc699b9510e5a2// Time: Tue Jul 23 14:43:37 2013 -0700//// However on previous versions, we have to work around it by ourselves.@OverridepublicvoidsetValue(String value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            super.setValue(value);
        } else {
            StringoldValue= getValue();
            super.setValue(value);
            if (!TextUtils.equals(value, oldValue)) {
                notifyChanged();
            }
        }
    }
}

When you need to use ListPreference in xml, simply replace the tag to com.example.yourapplication.ListPreferenceCompat.

This works neatly on my Samsung S4 device running Android 4.3, and inside one of my production app.

Solution 2:

I guess its some kind of a weird bug, because I've tested it on emulator running Android 2.3.3 and it didn't work, but when i tested it on 4.0.3 it worked. What i did was android:summary="%s" in xml for ListPreference and that's all.

Solution 3:

IMHO it is a bug. The onClickListener() defined in ListPreference.java#builder.setSingleChoiceItems() does not call notifyChanged()

I happen to be coding my own ListPreference based on that code, and ran across the same problem as the OP, I added notifyChanged():

            ...
            dialog.dismiss();
            notifyChanged(); // <<<<<<<<<<
        }
});

and now the preference summary is updated as soon as I choose it. It is not dependent on the Android version, ListPreference has been coded like this since ever.

Post a Comment for "Listpreference's Summary Text Is Not Updated Automatically Whenever There Is Change In Selection"