保存 checkedtextview 状态时出现问题,它对多个项目行为不当

Problem with saving checkedtextview state, it misbehaves with multiple items

我制作了一个包含 CheckTextView 和 TextView 的 activity。当用户选中该框时,我想在用户离开 activity 或关闭应用程序时保存该状态。

我为每个 CTV 添加了 onClickListener。

然后我尝试将其保存在onPause 和onResume 方法中。我无法解决这个问题,因为当我只保存其中的几个复选框时,它会起作用(它会有所不同,但它适用于其中的 1-5 个)但是当我添加所有它们时,当我返回时它们不会被保存activity。

//this will always work and will save the state of the boxes


protected void onPause() {
        super.onPause();

        save(ctv1.isChecked());
        save(ctv2.isChecked());
        save(ctv3.isChecked());


}

protected void onResume() {
        super.onResume();


        ctv1.setChecked(load());
        ctv2.setChecked(load());
        ctv3.setChecked(load());
}


//when I add all of them, they are always either checked or unchecked
//it doesn't matter what combination of them I try, it seems that it is //always working with a couple of CTV's but fails with more than 5-6 of them



//this is how my onClickListener looks like

     CheckedTextView ctv1 = (CheckedTextView) findViewById(R.id.ctvFOX1);
      ctv1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (ctv1.isChecked()) {
                  ctv1.setChecked(false);


              }
              else {
                  ctv1.setChecked(true);

              }
          }
      });




//save and load methods


private void save(final boolean isChecked) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.apply();
}

private boolean load() {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", false);
    }

因为你只用一键保存CheckedTextView's值!

 private void save(final boolean isChecked, String key) {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, isChecked);
        editor.apply();
    }

    private boolean load(String key) {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, false);
    }

    protected void onPause() {
        super.onPause();
        save(ctv1.isChecked(), "check1");
        save(ctv2.isChecked(), "check2");
        save(ctv3.isChecked(), "check3");



    }

    protected void onResume() {
        super.onResume();
        ctv1.setChecked(load("check1"));
        ctv2.setChecked(load("check2"));
        ctv3.setChecked(load("check3"));

    }