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