How to save checkbox value with shared preferences?

前端 未结 3 1205
你的背包
你的背包 2020-12-12 06:37

I don\'t know why my code not save checkbox checked value; if I check checkbox and after I click on another tab and comeback to previous tab,

相关标签:
3条回答
  • 2020-12-12 07:26
    @Override
    
        protected void onCreate(Bundle savedInstanceState) {
    
            // TODO Auto-generated method stub
    
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
    
    
            checkBox = (CheckBox) findViewById(R.id.checkBox1);                          
            SavedPreferences();
    
        }
    
    
        private void savePreferences(boolean value) {
    
            SharedPreferences sharedPreferences = PreferenceManager
    
                    .getDefaultSharedPreferences(this);
    
            Editor editor = sharedPreferences.edit();
    
            editor.putBoolean(value);
    
            editor.commit();
    
        }
    
    
    
    
    
    public class activity2 extends fragment{
    
           public onCreateView extends Fragment {
            SharedPreferences sharedPreferences = PreferenceManager
    
                    .getDefaultSharedPreferences(this);
    
            boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);    
    
            if (checkBoxValue) {
    
                checkBox.setChecked(true);
    
            } else {
    
                checkBox.setChecked(false);
    
            }
    
    
        }
    
    
    
        }
    
    0 讨论(0)
  • 2020-12-12 07:41
     @Override
     public void afterTextChanged(Editable s) {
                            p.setQuantità(finalHolder.edit.getText().toString().trim());
                            SharedPreferences preferences = getContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putString("KEY", finalHolder.edit.getText().toString().trim());
                            editor.putBoolean("CheckBox_Value", finalHolder.chkBox.isChecked());
                            editor.commit();
    
    }
    

    and then retrieve it using getBoolean as you have already done

    0 讨论(0)
  • 2020-12-12 07:42

    You can store boolean values on shared preferences. Here's an example.

    SharedPreferences myPrefs;
    SharedPreferences.Editor myPrefsPrefsEditor;
    static final String MY_SHARED_PREF = "name_of_your_shared_pref";
    

    Initialize your shared preferences

    myPrefs = this.getSharedPreferences(MY_SHARED_PREF, Context.MODE_PRIVATE);
    

    Store your values using the following.

    myPrefsPrefsEditor = myPrefs.edit();
    myPrefsPrefsEditor.putBoolean(key, value);
    myPrefsPrefsEditor.commit();
    

    key = use to find the value from shared pref

    value = the value you want to store

    This is how your read your values

    myPrefs.getBoolean(key, defaultValue);
    

    key = the key of the value you want to get

    defaultValue = the default value when there's no value for the given key.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题