Got Setsilent Error When Trying To Implement Sharedpreferences To Save State Of A Checkbox
I am trying to save the state of a checkbox while using SharedPreferences. I have spend hours on this problem...which I sure is easy to solve. I have been following the tutorial he
Solution 1:
You need to create a class variable which gets changed in your onCheckedChangedListener and then save it in shared preferences in your onStop() method.
Solution 2:
At the top of your activity declare a Boolean variable, lets call it checked.
Boolean checked;
It's good practice to initialize a variable so do that in you onCreate method.
checked = true;
In your onCheckedChanged method set the value of 'checked' to true or false depending on the state.
@OverridepublicvoidonCheckedChanged(CompoundButton buttonView,boolean isChecked) {
CheckBoxcheckBox= (CheckBox) expListView.findViewById(R.id.checkbox_tick);
checkBox.setOnCheckedChangeListener(this);
if (isChecked)
checked = true;
elsechecked=false;
}
In your onStop method save the value of your checked variable.
@OverridepublicvoidonStop(){
super.onStop();
SharedPreferences settings=getActivity().getSharedPreferences(suikodenprefs,Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= settings.edit();
editor.putBoolean("Tick the box",checked).commit();
}
Post a Comment for "Got Setsilent Error When Trying To Implement Sharedpreferences To Save State Of A Checkbox"