I am a beginner developer and have come across some issues when trying to implement SharedPreferences to save the state of a checkbox. I have been search Google a lot for an answer but with no luck. Im having trouble understanding what to and I have a few issues with my code.
Top part of my SuikodenFragment.java
public class SuikodenFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener{
private final String TAG = getClass().getSimpleName();
public static final String suikodenprefs = "SuikodenPrefs" ;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@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);
SharedPreferences settings = getSharedPreferences(suikodenprefs, 0);
boolean isChecked = settings.getBoolean("Tick the box", false);
setSilent(isChecked);
return view;
}
I am getting an error on getSharedPreferences - It says 'The method getSharedPreferences (String, int) is undefined for the type SuikodenFragment'
Further down in my SuikodenFragment.java
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
CheckBox checkBox = (CheckBox) expListView.findViewById(R.id.checkbox_tick);
checkBox.setOnCheckedChangeListener(this);
if (isChecked)
// Add the tick to the box
Log.d(TAG, "Tick the box");
else
// Remove the tick in the box
Log.d(TAG, "Untick the box");
}
@Override
public void onStop(){
super.onStop();
// SharedPreferences sharedpreferences = getSharedPreferences(suikodenprefs, Context.MODE_PRIVATE);
SharedPreferences settings = getSharedPreferences(suikodenprefs, 0);
settings.edit().putBoolean("Tick the box",true).commit();
}
The code above is the checkbox and the code to save the checkbox state, I hope. Again I am getting the same error on the getSharedPreferences. Any help would do here as I dont really understand SharedPreferences. Thanks in advance!