Home > DeveloperSection > Forums > Checkbox status is not saving in android
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    October-20-2014 12:12 AM

 Android Android 
Ratings:


 1 Reply(s)
 796  View(s)
Rate this:
I'm developing task list application using sharedpreferences. but it doesn't save checkbox status. I want to save when user click on checkbox. After user exits the application and see again it is already updated status. How to save checkbox status? Here is my code (Already Fixed and Working)

public class TaskList extends Activity {

            CheckBox cb1, cb2, cb3, cb4;

            Boolean checked = false;

 

            @Override

            protected void onCreate(Bundle savedInstanceState) {

                setContentView(R.layout.activity_tasks);

                super.onCreate(savedInstanceState);

                findID();

 

                cb1 = (CheckBox) findViewById(R.id.checkBox1);

                boolean isChecked = getBooleanFromPreferences("isChecked");

                Log.i("start", "" + isChecked);

                cb1.setChecked(isChecked);

                cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 

                    @Override

                    public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                        Log.i("boolean", "" + isChecked);

                        putBooleanInPreferences(isChecked,"isChecked");

 

                    }

                });

            }

 

            public void putBooleanInPreferences(boolean isChecked, String key) {

                SharedPreferences sharedPreferences = this

                        .getPreferences(Activity.MODE_PRIVATE);

                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putBoolean(key, isChecked);

                editor.commit();

            }

 

            private boolean getBooleanFromPreferences(String key) {

                SharedPreferences sharedPreferences = this

                        .getPreferences(Activity.MODE_PRIVATE);

                Boolean isChecked = sharedPreferences.getBoolean(key, false);

                return isChecked;

            }

 

            private void findID() {

                cb1 = (CheckBox) findViewById(R.id.checkBox1);

                cb2 = (CheckBox) findViewById(R.id.checkBox2);

                cb3 = (CheckBox) findViewById(R.id.checkBox3);

                cb4 = (CheckBox) findViewById(R.id.checkBox4);

            }

 

        }



Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    October-20-2014 12:14 AM

You need to update the value in SharedPreferencesevery time CheckBox check status is changed. Remove comment from this line Settings.this.putBooleanInPreferences(isChecked,"isChecked") in setOnCheckedChangeListener.

So, your listener will be as below :

cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 

            @Override

            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Log.i("boolean", "" + isChecked);

 

                putBooleanInPreferences(isChecked,"isChecked");

            }

        });

Hope this helps.


Don't want to miss updates? Please click the below button!

Follow MindStick