为什么这个偏好没有被保存?

时间:2014-08-01 17:30:24

标签: android android-preferences android-settings

据我所知,我在设置时使用相同的键,但偏好不保存。我确定我错过了一些明显的东西。

我的自定义DialogPreference,我加载当前首选项并设置新首选项。

public class SemesterDialogPreference extends DialogPreference {

    View view;
    SharedPreferences settings;
    String semester_id_key = this.getContext().getApplicationContext().getString(R.string.pref_semester_id);

    DBAdapter db;

    @Override
    public void onBindDialogView(View view){
            this.view = view;
            settings = getSharedPreferences();

            int curSemesterId = settings.getInt(semester_id_key, 1);
            System.out.println("Incoming " + semester_id_key + " equals " + curSemesterId);

            // Here I do stuff...

            super.onBindDialogView(view);
    }


    public SemesterDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.dialog_set_semester);

    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        // Do things and set newId


        System.out.println("new int for " + semester_id_key + " is " + newId);
        Editor editor = getEditor();
        editor.putInt(semester_id_key, newId);
        editor.commit();
    }
}

我的PreferenceScreen的XML

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="@string/pref_header_settings" >
            <com.NsouthProductions.gradetrackerpro.SemesterDialogPreference
                    android:title="@string/pref_title_semester"
                    android:summary="@string/pref_summary_semester"

                    android:key="@string/pref_semester_id"
                    android:dialogMessage="Are you sure?"
                    android:positiveButtonText="Clear Questss"
                    android:negativeButtonText="Cancel"/>

    </PreferenceCategory>

</PreferenceScreen>

此外,我不明白为什么examples online不包含getSharedPreferences()中的参数。我认为这就是android如何知道要使用哪个文件。

3 个答案:

答案 0 :(得分:1)

以下是write to SharedPreferences

的方法
    public static final String PREFS_NAME   = "UNIQUE-NAME"; // settings will be saved here 

    // ...       
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    Editor editor = settings.edit();
    editor.putString("abc", "123");
    editor.commit();

如果您收到错误:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

你可以这样做:

SharedPreferences settings = getActivity().getPreferences(PREFS_NAME, 0);

如果您尝试从其他活动接近它,请执行以下操作:

SharedPreferences settings = getSharedPreferences(YourBaseActivity.PREFS_NAME, 0);

(不用说,您应该将YourBaseActivity替换为您声明SharedPreferences开头的活动的名称。

答案 1 :(得分:0)

参考http://developer.android.com/guide/topics/data/data-storage.html

这样做:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("silentMode", mSilentMode);

  // Commit the edits!
  editor.commit();

答案 2 :(得分:0)

您需要像这样调用编辑器:

...

View view;
SharedPreferences settings;
SharedPreferences.Editor editor;

...

然后在下一部分:

settings = PreferenceManager.getDefaultSharedPreferences(this); // if this doesnt work, try getApplicationContext()

editor = settings.edit();
editor.putInt(semester_id_key, newId);
editor.commit();

注意:

建议尽可能使用apply()而不是commit(),因为它是异步的,但两者都有效。