I have prepared a simple test case for my problem at GitHub.
I have extended a DialogPreference to a custom NumberPickerPreference - and it works well: displays a NumberPicker
in a dialog and saves integer value when the dialog is closed.
My problem is that the saved integer value is never used to set the NumberPicker
- when the dialog opened:
In the above screenshot you can see (in the summary field at the bottom) that the saved integer value is 14 (and I see that value being passed to mPicker.setValue(mNumber)
call in debugger) - but the NumberPicker
is still scrolled to 1.
Here is the problematic NumberPickerPreference.java:
public class NumberPickerPreference extends DialogPreference implements OnValueChangeListener {
private NumberPicker mPicker;
private Integer mNumber = 0;
private Integer mPickerValue = 0; // keeps picker value before dialog closes
public NumberPickerPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDialogLayoutResource(R.layout.preference_picker);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mPicker = (NumberPicker) view.findViewById(R.id.picker);
mPicker.setValue(mNumber);
mPicker.setMinValue(1);
mPicker.setMaxValue(100);
mPicker.setOnValueChangedListener(this);
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mPickerValue = newVal;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
setValue(mPickerValue);
}
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue);
}
public void setValue(int value) {
if (shouldPersist()) {
persistInt(value);
}
if (value != mNumber) {
mNumber = value;
notifyChanged();
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, 0);
}
}
And here is the layout file preference_picker.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="48dp"
android:layout_marginTop="48dp"
android:overScrollMode="ifContentScrolls" >
<NumberPicker
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
Does anybody please know, what is missing here?
Initially I thought, that adding onGetDefaultValue
method would help - like in my other question (Custom inline SeekBarPreference - how to set SeekBar progress on the 1st run?) - but it hasn't.