Shared preferences saving wrong integer

1

I'm saving the user code (an integer) in the shared preferences:

      String a = Integer.toString(pUsuario.getCodigo());

    SharedPreferences.Editor editor_Codigo = oCodigo.edit();
    editor_Codigo.putString(pContext.getString(R.string.codigo_usuario), a);
    editor_Codigo.apply();

To recover I use:

   SharedPreferences oCodigo = pContext.getSharedPreferences(pContext.getString(R.string.codigo_usuario), Context.MODE_PRIVATE);
    String a = String.valueOf(oCodigo.getString(pContext.getString(R.string.codigo_usuario), "0"));
    return Integer.parseInt(a);

But no matter what code you save, at the time of returning always returns the code "2"

    
asked by anonymous 18.05.2018 / 15:42

1 answer

1

Try this:

private String KEY_CODIGO = "KEY_CODIGO";

SharedPreferences.Editor editor_Codigo = oCodigo.edit();
editor_Codigo.putString(KEY_CODIGO , pUsuario.getCodigo());
editor_Codigo.commit();

In search of value:

SharedPreferences oCodigo = pContext.getSharedPreferences("file.preferences", Context.MODE_PRIVATE);
String codigo = oCodigo.getString(KEY_CODIGO , ""));
return Integer.parseInt(codigo);

I recommend using SharedPreferences in a class to only do this search to facilitate future problems like this. I use this class in my app, stay free to copy, edit, distribute, etc.

public class Preferences {

    private Context mContext;

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    private String FILE_NAME = "app.Preferences";
    private int MODE = Context.MODE_PRIVATE;

    private String KEY_LOGGEDUSERID = "KEY_LOGGEDUSERID";
    private String KEY_LOGGEDUSERNAME = "KEY_LOGGEDUSERNAME";
    private String KEY_EMERGENCYID = "KEY_EMERGENCYID";
    private String KEY_EMERGENCYNAME = "KEY_EMERGENCYNAME";
    private String KEY_TIMEDELAY = "KEY_TIMEDELAY";

    public Preferences(Context context) {
        mContext = context;
        preferences = mContext.getSharedPreferences(FILE_NAME, MODE);
        editor = preferences.edit();
    }

    public void setLoggedUserId(String id) {
        editor.putString(KEY_LOGGEDUSERID, id);
        editor.commit();
    }

    public String getLoggedUserId() {
        return preferences.getString(KEY_LOGGEDUSERID, "");
    }

    public void setLoggedUserName(String name) {
        editor.putString(KEY_LOGGEDUSERNAME, name);
        editor.commit();
    }

    public String getLoggedUserName() {
        return preferences.getString(KEY_LOGGEDUSERNAME, "");
    }

    public void setEmergencyUserId(String id) {
        editor.putString(KEY_EMERGENCYID, id);
        editor.commit();
    }

    public String getEmergencyUserId() {
        return preferences.getString(KEY_EMERGENCYID, "");
    }

    public void setTimeDelay(String time) {
        editor.putString(KEY_TIMEDELAY, time);
        editor.commit();
    }

    public String getTimeDelay() {
        return preferences.getString(KEY_TIMEDELAY, "");
    }


    public void setEmergencyUserName(String name) {
        editor.putString(KEY_EMERGENCYNAME, name);
        editor.commit();
    }

    public String getEmergencyUserName() {
        return preferences.getString(KEY_EMERGENCYNAME, "");
    }

}

When I need to retrieve the value just use:

Preferences preferences = new Preferences(this);
String loggedUserId = preferences.getLoggedUserId();
    
18.05.2018 / 20:55