Save Checkboxes Values in Android Studio

2

I'm in a project that has many checkboxes, and they have to be stored definitively, ie when the user close the app and reopen it has to be checked. Would you like to know how to save checkbox modes, definitely, in the app created in Android Studio? or to turn Checkboxes set to text, since they will not be cleared again.

I will await your help.

    
asked by anonymous 14.09.2015 / 15:38

2 answers

2

You can save to the database, or use SharedPreferences , which I believe is the simplest way:

To facilitate, I recommend creating a class that contains the values you wanted to save:

class Valores
{
    boolean check1;
    boolean check2;
    boolean check3;
    boolean check4;
}

Method to save this class:

   /**
     * seta os valores no SharedPreferences
     * @param valores
     */
    public void setValores(Valores valores)
    {
        // passamos o nome do objeto que vamos criar e o modo de armazenamento
        final SharedPreferences prefs = getApplicationContext().getSharedPreferences("VALORES_TELA_UM", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = prefs.edit();
        // setamos o nome da propriedade e o valor
        editor.putBoolean("CHECK1", valores.check1);
        editor.putBoolean("CHECK2", valores.check2);
        editor.putBoolean("CHECK3", valores.check3);
        editor.putBoolean("CHECK4", valores.check4);
        // commitando as alteracoes
        editor.commit();

    }

Method to redeem values:

  public Valores getValores()
    {
        Valores valores = new Valores();

        final SharedPreferences prefs = getApplicationContext().getSharedPreferences("VALORES_TELA_UM", Context.MODE_PRIVATE);

        valores.check1 = prefs.getBoolean("CHECK1", false);
        valores.check2 = prefs.getBoolean("CHECK2", false);
        valores.check3 = prefs.getBoolean("CHECK3", false);
        valores.check4 = prefs.getBoolean("CHECK4", false);

        return valores;
    }
    
14.09.2015 / 15:55
1

Android's api has ways for you to save your app's preferences as key / value sets. Check out the Saving Key-Value Sets documentation.

To create the preferences file (in which case checked checkbox):

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

Where R.string.preference_file_key is a string that you declare with the name of the file that will contain the key / value sets.

To save a set value key:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.show_score), true);
editor.commit();

Where R.string.show_score is a string with the name of your key, which in your case could be chkRodaEmModoNavio .

To read your preferences:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean defaultValue = getResources().getBoolean(R.string.show_score_default);
boolean showHighScore = sharedPref.getBoolean(getString(R.string.show_score), defaultValue);
    
14.09.2015 / 15:57