简体   繁体   中英

How to use SharedPreferences to save my checkbox state in Android Xamarin

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

        };         


    }

Use this way

To save bool value

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("bool_value", mBool);
editor.Commit();  

To Retrive the bool value

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
mBool = prefs.GetBoolean ("bool_value", <default value>);

@Ironman here is the code.

 CheckBox checkbox;
    ImageView imageView;
    int estado = 0;
    private ISharedPreferencesEditor editor;
    private ISharedPreferences prefs;


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Pagina_1);

        //here i am trying to get the value of estado for my checkbox state
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        estado = prefs.GetInt("int_value", 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);
            }

            //THE ERRO CS0136 IS IN THIS prefs
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutInt("int_value", estado);
            editor.Commit();

        };




    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM