lundi 19 juin 2017

Android - Saving the state of checkboxes not working sharedpreferences

I'm trying to save the state of checkboxes and the checkboxes have 3 checked states (red, yellow, green) in a fragment. I am trying shared preferences, but it is not saving in the app even when going to different tabs. I have 4 fragments that goes from fragment to fragment and ends with the 4th fragment with the checkboxes. You can go back to the 3rd fragment with a button. I need to be able to save the checkboxes if they are red, yellow, or green, and when the user goes anywhere in the app, when they come back to the checklist, the checkbox will either be red, yellow, or green depending on how many times the tapped the checkbox. It seems to not be saving anything, and it seems to be resetting when I go to other places in the app. Here is my code:

public class CheckListGatherFragment extends Fragment{
    private CheckBox chkStart;
    private int countStart;
    private int start;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor editor;
    private Button btnGoBackToCheck;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_checklist_gather, container, false);
        chkStart = (CheckBox) rootView.findViewById(R.id.chkStart);
        btnGoBackToCheck = (Button) rootView.findViewById(R.id.btnGoBackToChecklist);

        mSharedPreferences = CheckListGatherFragment.this.getActivity().getSharedPreferences("preferences_key", Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();

        setOnClick();

        return rootView;
    }

    public void setOnClick() {
        btnGoBackToCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            getFragmentManager().beginTransaction().replace(R.id.fragment_checklist_gather, new CheckListScreenMainFragment()).addToBackStack("Gather").commit();
            }
        });
        chkStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countStart = checked(chkStart, countStart);
                editor.putInt("checkStartId", countStart);
                editor.apply();
            }
        });
    }

    public int checked(CheckBox checkBox, int count){
        if (checkBox.isChecked()){
            count++;
        } else if (!checkBox.isChecked()){
            checkBox.setChecked(true);
            count++;
        }
        if (count == 1){
            checkBox.setButtonDrawable(R.drawable.custom_yellow_checkbox);
        }
        if (count == 2){
            checkBox.setButtonDrawable(R.drawable.custom_green_checkbox);
        }
        if (count > 2){
           count = 0;
           checkBox.setButtonDrawable(R.drawable.custom_red_checkbox);
        }
        return count;
     }

    @Override
    public void onResume() {
        super.onResume();
        start = mSharedPreferences.getInt("CheckStartId", 0);
        if (start == 0){
            chkStart.setButtonDrawable(R.drawable.custom_red_checkbox);
        }if (start == 1){
            chkStart.setButtonDrawable(R.drawable.custom_yellow_checkbox);
        }if (start == 2){
            chkStart.setButtonDrawable(R.drawable.custom_green_checkbox);
        }
    }
}

Any help would be much appreciated. Thanks!




Aucun commentaire:

Enregistrer un commentaire