I have two activitys, the MainActivity and Pagina_1Activity.
In MainActivity i have a button that goes to the Pagina_1Activity using Intent. In Pagina_1Activity i have a checkbox and a imageView. When i check the checkbox it changes the image in imageView and that is working great.
What i am trying to do is to save the checkbox state using the SharedPreferences, and i am trying to do that using an integer value, the "int estado".
What i want to do is: if i go back to MainActivity or close de app, it saves the checkbox (if it is checkd stay checked).
Can anyone help me with this and tell me why my sharedpreferences dont work? I am trying for days .. with no solution...
CheckBox checkbox;
ImageView imageView;
int estado;
private ISharedPreferencesEditor editarEstado;
private ISharedPreferences valorEstado;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Pagina_1);
//here i am trying to get the value of the estado for my checkbox state
valorEstado = Application.Context.GetSharedPreferences("valorE", FileCreationMode.Private);
var pegar = valorEstado.GetInt("Estado", 0);
checkbox = FindViewById<CheckBox>(Resource.Id.checkBox1);
imageView = FindViewById <ImageView>(Resource.Id.imageView1);
checkbox.Click += delegate
{
if (checkbox.Checked)
{
estado = 1;
}
else
{
estado = 0;
}
if (estado == 1)
{
checkbox.Checked = true;
imageView.SetImageResource(Resource.Drawable.guildwars_icone);
}
if (estado == 0)
{
checkbox.Checked = false;
imageView.SetImageResource(Resource.Drawable.guardian);
}
//here i am trying to save the value of estado for my checkbox state
valorEstado = Application.Context.GetSharedPreferences("valorE", FileCreationMode.Private);
editarEstado = valorEstado.Edit();
editarEstado.PutInt("Estado", estado);
editarEstado.Commit();
};
}
Aucun commentaire:
Enregistrer un commentaire