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 here
However, I do not understand what setSilent is and what I have to change it to within my own code. setSilent has been changed to checked in the below code based on one of the answers. I have searched through stackoverflow a lot and found loads of related answers but nothing has worked and the setSilent (sometimes a different name) error is always persistent. I have posted my code below.
SuikodenFragment.java -- The error is here.
Boolean checked;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.suikoden_main_activity1, container, false);
// you can use findViewById() using the above 'view'
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.suikodenList1);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
checked = true;
return view;
}
public void onStart() {
super.onStart(); // Always call the superclass method first
SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, Context.MODE_PRIVATE);
boolean isChecked = settings.getBoolean("Tick the Box", checked);
checked(isChecked);
//if(isChecked)
//checkBox.setChecked(true);
}
The code below is placing the checkbox state which might help understand my problem.
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
CheckBox checkBox = (CheckBox) expListView.findViewById(R.id.checkboxTick);
checkBox.setOnCheckedChangeListener(this);
if (isChecked)
// Add the tick to the box
//Log.d(TAG, "Tick the box");
checked = true;
else
// Remove the tick in the box
//Log.d(TAG, "Untick the box");
checked = false;
}
@Override
public void onStop(){
super.onStop();
SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Tick the Box",checked).commit();
}
I really hope someone can help with this. It may seem like a stupid problem but if it can be solved then I can complete a huge section of my app. The following link is the closest to my own code...but when i implement their solution, checkBoxView flags up as an error Android saving state of checkbox if exit app. I am a beginner so I apologise...thanks in advance!
At the top of your activity declare a Boolean variable, lets call it checked.
It's good practice to initialize a variable so do that in you onCreate method.
In your onCheckedChanged method set the value of 'checked' to true or false depending on the state.
In your onStop method save the value of your checked variable.
You need to create a class variable which gets changed in your onCheckedChangedListener and then save it in shared preferences in your onStop() method.