CHECKBOX STATUS IS NOT SAVING IN ANDROID

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
 1047  View(s)
Ratings:
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);

            }

 

        }

  1. Ankita Pandey

    Post:183

    Points:1285
    Re: Checkbox status is not saving in android

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!