KarlKarlsom
KarlKarlsom

Reputation: 5868

NumberPickerPreference - Default value is not loaded

I would like to use a NumberPicker preference.

Basically the code is working as expected. A dialog opens with a NumberPicker. The value can be selected and is saved to the defaulSharedPreferences.

But on the first time the PreferenceActivity is started the default Value is not loaded and I cant figure out why.

Behavior is like this: When I open the PreferenceActivity the summary of the NumberPickerPreference shows -1. When I close the Activity and reopen it again the value stays at -1 (This is as long the defaultSharedPreferences has no Value under the key stored). As soon a Value is selected by the user (or a Value is saved by code under the key into the defaultSharedPreferences) everthing works and the value is loaded when the PreferenceActivity is started.

    public class NumberPickerPreference extends DialogPreference implements NumberPicker.OnValueChangeListener
    {
        private static final String NAMESPACE="http://schemas.android.com/apk/res/android";

        private NumberPicker mNumberPicker;
        private TextView mTvDialogMessage;
        private Context mCtx;

        private String mDialogMessage;
        private int mDefault;
        private int mMax;
        private int mValue = 0;

        public NumberPickerPreference(Context ctx, AttributeSet attr) { 
            super(ctx, attr); 
            mCtx = ctx;

            //Get XML attributes
            mDialogMessage = attr.getAttributeValue(NAMESPACE,"dialogMessage");
            mDefault = attr.getAttributeIntValue(NAMESPACE,"defaultValue", 2);
            mMax = attr.getAttributeIntValue(NAMESPACE,"max", 20);
        }

        @Override 
        protected View onCreateDialogView() {
            //Create Views
            LinearLayout dialogLayout = new LinearLayout(mCtx);
            mTvDialogMessage = new TextView(mCtx);
            mNumberPicker = new NumberPicker(mCtx);

            //Set View attributes
            dialogLayout.setOrientation(LinearLayout.VERTICAL);
            if (mDialogMessage!=null)
                mTvDialogMessage.setText(mDialogMessage);
            dialogLayout.addView(mTvDialogMessage);
            mNumberPicker.setOnValueChangedListener(this);
            dialogLayout.addView(mNumberPicker, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            if (shouldPersist())
                mValue = getPersistedInt(mDefault);
            mNumberPicker.setMaxValue(mMax);
            mNumberPicker.setMinValue(1);
            mNumberPicker.setValue(mValue);
            return dialogLayout;
        }

        @Override 
        protected void onBindDialogView(View v) {
            super.onBindDialogView(v);
            mNumberPicker.setMaxValue(mMax);
            mNumberPicker.setMinValue(1);
            mNumberPicker.setValue(mValue);     
            setSummary(mValue);
        }

        @Override
        protected void onSetInitialValue(boolean restore, Object defaultValue)  
        {
            super.onSetInitialValue(restore, defaultValue);
            if (restore) 
                mValue = shouldPersist() ? getPersistedInt(mDefault) : 2;
            else 
                mValue = (Integer)defaultValue;
            if (mNumberPicker!=null)
                mNumberPicker.setValue(mValue);
            setSummary(mValue);
        }

        public void setSummary(int value) {
            CharSequence summary = getSummary();
            value=getPersistedInt(-1);
            if (summary == null) {
                setSummary(Integer.toString(value));
            } else {
                setSummary(String.format(summary.toString(), value));
            }
        }

        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            setSummary(newVal);
            if (shouldPersist())
                persistInt(newVal);
            callChangeListener(new Integer(newVal));    
        }
    }

Upvotes: 0

Views: 1975

Answers (2)

X-Frox
X-Frox

Reputation: 419

I was experiencing the same issue. Not sure if it is correct but calling

setDefaultValue(Object);

with my default value in Preference constructor seemed to solve this.

Upvotes: 2

Kendroid
Kendroid

Reputation: 116

Try adding notifyChanged() after persistInt(newVal)

Upvotes: 0

Related Questions