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();
Why convert the integer to
String
and useputString
when you can use directlyputInt
? After the lineString a = Integer.toString(pUsuario.getCodigo());
thata
has what value ?– Isac
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– Marceloawq
This will be for another reason. The first parameter of the
putString
, as well as theputInt
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.– Isac
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
– Marceloawq
And how are you doing this
getInt
? I started by simplifying all these puts and gets to use direct names with something likeeditor_Codigo.putInt("codigo_usuario",pUsuario.getCodigo());
. Translation of strings ai is only complicating and not necessary for this purpose.– Isac
using the name of the direct strings worked. But then what is the purpose of making a
getstring
?– Marceloawq
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 theputInt
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.– Isac
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.
– Marceloawq
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 doputInt("usuario", 10);
And in a little while, when you go to get it, you’re doinggetInt("user", 10)
because you got the newString
based on the translation applied. You can always take the original code you had and doLog
of the variousStrings
which is catching onpContext.getString(R.string.codigo_usuario)
to isolate the problem.– Isac