【问题标题】:Android - Saving the state of checkboxes not working sharedpreferencesAndroid - 保存复选框的状态不起作用 sharedpreferences
【发布时间】:2017-11-22 02:12:19
【问题描述】:

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

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

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android android-fragments checkbox sharedpreferences


    【解决方案1】:

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

    【讨论】:

    • 我已经尝试过了,但没有成功。这就是我选择 apply 的原因。
    【解决方案2】:

    您使用的密钥大写错误。在一处使用checkStartId,而在另一处使用CheckStartId。使用其中之一,但不能同时使用。

    专业提示:将您的键声明为顶部的最终变量,以防止出现这样的拼写错误:

    private static final String CHECK_START_ID = "checkStartId"

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

    【讨论】:

    • 非常感谢!做到了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多