by what I see you are not saving anything in your sharedpreferences I have an example for you I used this way in my application..
I created a class of "preference"
public class Preferencia {
public static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences("energy", Context.MODE_PRIVATE);
}
public static String getlogin(Context context) {
SharedPreferences sharedPref = getSharedPreferences(context);
return sharedPref.getString("Login", "");
}
//// preferencia de senha
public static void setsenha(Context context, String senha) {
SharedPreferences sharedPref = getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Senha", senha);
editor.commit();
}
public static String getsenha(Context context) {
SharedPreferences sharedPref = getSharedPreferences(context);
return sharedPref.getString("Senha", "");
}
public static boolean setcbremember(Context context, boolean a) {
SharedPreferences sharedPref = getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Cbremember", a);
editor.commit();
return a;
}
public static boolean getcbremember(Context context) {
SharedPreferences sharedPref = getSharedPreferences(context);
return sharedPref.getBoolean("Cbremember", true);
}
}
and where I want you to save something I do so
public void btnentrarclick(View currentButton)
{
if(cbremember.isChecked()) {
AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
edtlogin.getText().toString(), edtsenha.getText().toString());
Preferencia.setlogin(Tela_de_Login.this, edtlogin.getText().toString());
Preferencia.setsenha(Tela_de_Login.this, edtsenha.getText().toString());
Preferencia.setcbremember(Tela_de_Login.this, cbremember.isChecked());
ac.execute("");
}
In my case I saved login and password of the user so that it does not need to be typing all the time
and when I want to erase my user data and password I do so
else{
AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
edtlogin.getText().toString(), edtsenha.getText().toString());
Preferencia.deletar(Tela_de_Login.this, edtlogin.getText().toString());
Preferencia.deletar(Tela_de_Login.this, edtsenha.getText().toString());
ac.execute("");
}
and in the preference have these codes to delete the information if it takes the option of the checkbox in my example of course...
//// Deletar preferencia de login e senha
public static void deletar(Context context, String login) {
SharedPreferences sharedPref = getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.commit();
}
I hope I’ve helped..
It would be nice to show how you’re making these recordings on
Editor
, but I believe that Tiago’s answer should already solve the problem.– Wakim