Save SharedPreferences by assigning a key using getDefaultSharedPreferences ()

10

I have a class to save the email and user that connect in the application with sharedpreferences , but I save the data without any key reference so when I want to save a different data without overwriting the previous one I do not it does, I guess I need to assign some key to reference it and be able to save the data I want without stepping on the previous one and giving it a name:

public void save(Context context, String text) {
    SharedPreferences settings;
    Editor editor;

    settings = PreferenceManager.getDefaultSharedPreferences(context);

    editor = settings.edit();

    editor.putString(PREFS_KEY, text);

    editor.commit();
}

Here I call the function:

String email = _emailText.getText().toString();
sharedPreference.save(this, email);
    
asked by Fen Dev 27.04.2016 в 00:32
source

2 answers

6

I do not think that you are not saving the value, the problem is that you have to get the saved value in this way, (assuming that your key is email )

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String email = preferences.getString("email, "");

Remember that you use getDefaultSharedPreferences which does not require a name for the preferences file, but its preference values if.

I add how it is done with the two methods:

Save and get a value using getDefaultSharedPreferences ( )

public void saveValuePreference(Context context, String text) {
    SharedPreferences settings;
    SharedPreferences.Editor editor;
    settings = PreferenceManager.getDefaultSharedPreferences(context);
    editor = settings.edit();
    editor.putString("email", text);
    editor.commit();
}

public String getValuePreference(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return  preferences.getString("email", "");
}

Save and get a value using getSharedPreferences ()

private String PREFS_KEY = "mispreferencias";

public void saveValuePreference(Context context, String text) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    SharedPreferences.Editor editor;
    editor = settings.edit();
    editor.putString("email", text);
    editor.commit();
}



public String getValuePreference(Context context) {
    SharedPreferences preferences = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    return  preferences.getString("email", "");
}
    
answered by 27.04.2016 / 00:57
source
3

You have to use android.content.SharedPreferences.Editor; Here I give you an example of a class with two methods that keeps shared preferences with a key:

public class AlmacenPuntuacionesPreferencias implements AlmacenPuntuaciones {
    private static String PREFERENCIAS="puntuaciones";
    private Context context;
    public AlmacenPuntuacionesPreferencias(Context context) {
        this.context=context;
    }
    @Override
    public void guardaPuntuacion(int puntos, String nombre, long fecha) {
        SharedPreferences preferencias=context.getSharedPreferences(PREFERENCIAS,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferencias.edit();
        for (int n = 9; n >=1; n--) {
            editor.putString("puntuacion"+n,preferencias.getString("puntuacion"+(n-1),""));

        }
        editor.putString("puntuacion0", puntos+" "+nombre);
        editor.commit();
    }

    @Override
    public Vector<String> listaPuntuaciones(int cantidad) {
        Vector<String> result=new Vector<String>();
        SharedPreferences preferencias=context.getSharedPreferences(PREFERENCIAS,Context.MODE_PRIVATE);
        for (int n = 0; n <=9; n++) {
            String s=preferencias.getString("puntuacion"+n,"");
            if(s!=""){
                result.add(s);
            }
        }
        return result;
    }

}

I think this can now be adapted to your needs.

Greetings

    
answered by 27.04.2016 в 00:50