Android - 保存复选框的状态不起作用共享偏好

时间:2017-06-19 18:41:17

标签: android android-fragments checkbox sharedpreferences

我正在尝试保存复选框的状态,复选框在片段中有3个已检查状态(红色,黄色,绿色)。我正在尝试共享首选项,但即使进入不同的选项卡,也不会在应用程序中保存。我有4个片段,从片段到片段,最后是带有复选框的第4个片段。您可以使用按钮返回第3个片段。我需要能够保存复选框,如果它们是红色,黄色或绿色,当用户进入应用程序的任何位置时,当他们返回核对清单时,复选框将为红色,黄色或绿色,具体取决于点击复选框多少次。它似乎没有保存任何东西,而且当我去应用程序中的其他地方时它似乎正在重置。这是我的代码:

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);
        }
    }
}

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用 editor.commit(); 代替 editor.apply();

答案 1 :(得分:0)

您正在使用错误的大写键。在一个地方,您使用checkStartId而在另一个地方使用CheckStartId。使用其中一个,但不是两个。

专业提示:将您的密钥声明为最终变量,以防止这样的拼写错误:

private static final String CHECK_START_ID = "checkStartId"

现在当你需要密钥时,不要输入字符串,而是使用字符串变量