0

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!

theCreed
  • 55
  • 11
  • @Jave Thanks, I combined what Irshad Khan suggested but I still dont know what to do with setSilent...any ideas? – theCreed Jun 14 '14 at 13:58

1 Answers1

1

replace your code by this code

SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, 0);

hope it will help you!

Irshad Khan
  • 794
  • 6
  • 21
  • thanks for the response. It has fixed my issue with getSharedPreferences. But I still do not know what to do with setSilent. Any ideas? – theCreed Jun 14 '14 at 13:59
  • @theCreed what is setSilent? – zgc7009 Jun 14 '14 at 17:59
  • @zgc7009 I have just posted a more specific question as my problem in this one has been answered, thanks for looking into it http://stackoverflow.com/questions/24222780/got-setsilent-error-when-trying-to-implement-sharedpreferences-to-save-state-of – theCreed Jun 14 '14 at 18:11