0

据我所知,我在获取时使用与设置时相同的键,但首选项不会保存。我确定我遗漏了一些明显的东西。

我的自定义 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>

另外,我不明白为什么在线示例不包含getSharedPreferences(). 我认为这就是android知道要使用哪个文件的方式。

4

3 回答 3

1

Here's how to 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();

In case you get an error on:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

you can do:

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

and if you're trying to approach it from a different activity, then do:

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

(needless to say that you should replace YourBaseActivity with the name of the activity in which you declared the SharedPreferences to begin with).

于 2014-08-01T18:07:27.993 回答
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();
于 2014-08-01T17:41:13.173 回答
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()尽可能使用,因为它是异步的,但两者都可以工作。

于 2014-08-01T17:42:43.147 回答