Question

I'm first using Android Preferences and encountered unexpected problem.

I'm extend DialogPreference class and all works fine except one thing: in method onDialogClosing(boolean positiveResult) I'm receiving false no matter what button I'v pressed. What I'm doing wrong?

Whole code of the class is listed below.

package edu.kpi.ept.labwork1;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

public class PositivePickerPreference extends DialogPreference {

private static int DEFAULT_VALUE = 0;

private int selectedValue;
private EditText intEdit;

public PositivePickerPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setDialogLayoutResource(R.layout.int_pick_pref_dialog);
    this.setPositiveButtonText(R.string.preference_ok);
    this.setNegativeButtonText(R.string.preference_cancel);
}

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);
    intEdit = (EditText) view.findViewById(R.id.intEdit);
    selectedValue = getPersistedInt(DEFAULT_VALUE);
    intEdit.setText(Integer.toString(selectedValue));
}

public void onClick (DialogInterface dialog, int which) {
    super.onClick();
    selectedValue = Integer.parseInt(intEdit.getText().toString());
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        persistInt(selectedValue);
    }
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
    super.onSetInitialValue(restorePersistedValue, defaultValue);
    if (restorePersistedValue) {
        selectedValue = getPersistedInt(DEFAULT_VALUE);
    } else {
        selectedValue = (Integer) defaultValue;
        persistInt(selectedValue);

    }
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getInteger(index, DEFAULT_VALUE);
}

}
Was it helpful?

Solution

Just had this same issue. Its because of the onClick handler:

public void onClick (DialogInterface dialog, int which) {
    super.onClick();
    selectedValue = Integer.parseInt(intEdit.getText().toString());
}

Remove it, and you won't have the issue. If you need to know the button pressed, then just check the button type in that event handler block. For example

@Override
public void onClick(DialogInterface dialog, int which) {
    buttonPress = which;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

if (buttonPress == DialogInterface.BUTTON_NEGATIVE) {
            String computerName = _etComputerName.getText().toString();
            SharedPreferences computers = _context.getSharedPreferences(
                    "COMPUTERS", 0);
            SharedPreferences.Editor editor = computers.edit();
            editor.remove(computerName);
            editor.commit();
            this.callChangeListener(-1);
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top