Shared preferences saving whole wrong

Asked

Viewed 74 times

1

I am saving the user code (an integer) in 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 return always returns the code "2"

  • Why convert the integer to String and use putString when you can use directly putInt ? After the line String a = Integer.toString(pUsuario.getCodigo()); that a has what value ?

  • because I’m saving in the strings ~> pContext.getString(R.string.codigo_usuario when using putint gives an error. "a" has the right value of the code, type 109. Only when recovering it returns 2

  • This will be for another reason. The first parameter of the putString, as well as the putInt is the name that will be associated with the value that is being saved. And as a general rule you want a deterministic name and not based on language because this is a name that will not be visible to the user and only for internal use of the application.

  • I made the putint, it saves the value. Then when I give getint gives the exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

  • And how are you doing this getInt ? I started by simplifying all these puts and gets to use direct names with something like editor_Codigo.putInt("codigo_usuario",pUsuario.getCodigo());. Translation of strings ai is only complicating and not necessary for this purpose.

  • using the name of the direct strings worked. But then what is the purpose of making a getstring?

  • The purpose of pContext.getString(R.string.codigo_usuario) is to obtain texts to show the user, which can be translated based on the language the device detected. The text that goes to the putInt is the name intern associated with the value you keep. This should not be translated because it is an internal name, so it makes no sense to translate.

  • For me it does not make sense to have this method and not be possible to take the value associated with this string, but okay, solved the problem.

  • The problem is that using it makes the String where you will find the potentially dynamic content, which may change with the use. It means that you now do putInt("usuario", 10); And in a little while, when you go to get it, you’re doing getInt("user", 10) because you got the new String based on the translation applied. You can always take the original code you had and do Log of the various Strings which is catching on pContext.getString(R.string.codigo_usuario) to isolate the problem.

Show 4 more comments

1 answer

1

Try it like 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 advise using the Sharedpreferences in a class only to do this search to facilitate future problems like this. I use in my app this class, be 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 recover the value just use:

Preferences preferences = new Preferences(this);
String loggedUserId = preferences.getLoggedUserId();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.