0

As far as I can tell, I am using the same key when getting as when setting, yet the preference does not save. I'm sure I'm missing something obvious.

My custom DialogPreference where I load the current preference and set the new one.

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();
    }
}

The XML for my PreferenceScreen

<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>

Also, I don't understand why examples online don't include parameters in getSharedPreferences(). I thought that's how android knew which file to use.

Community
  • 1
  • 1
NSouth
  • 5,067
  • 7
  • 48
  • 83

3 Answers3

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).

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • This is the solution that worked for me! Thanks. Also, I apparently had my Main activity resetting this preference, so it's possible one of the other solutions would have worked, too. – NSouth Aug 01 '14 at 19:25
0

reffering to http://developer.android.com/guide/topics/data/data-storage.html

do it like:

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

  // Commit the edits!
  editor.commit();
LibettenRadler
  • 115
  • 2
  • 8
  • I don't think this works in a DialogPreference. Using that version of `getSharedPreferences` gives me an error. _The method getSharedPreferences() in the type Preference is not applicable for the arguments (String, int)_ – NSouth Aug 01 '14 at 17:57
0

You need to call the editor like this:

...

View view;
SharedPreferences settings;
SharedPreferences.Editor editor;

...

Then in the next part:

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

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

Note:

It's recommended to use apply() rather than commit() whenever possible, because it is asynchronous, but both will work.

SQLiteNoob
  • 2,958
  • 3
  • 29
  • 44
  • Thanks. I declared view, settings, and editor as fields as you suggested. I initialized settings using PreferenceManager in `onBindDialogView` as you suggested. In `onDialogClosed`, I used the editor field as you suggested. Now I get a NullPointerException on the `putInt` line :( – NSouth Aug 01 '14 at 18:02