Checkbox SharedPreference在Recyclerview复选框中不起作用

时间:2015-12-16 06:04:24

标签: android android-sharedpreferences

我正在尝试在我的RecyclerView适配器中实施 SharedPreference用于存储复选框选中列表。 我能够正确记录检查列表数组。但我无法将其保存为共享首选项,并在应用每次重新打开时恢复它。

这是我的RecyclerView适配器代码。

public class HomeManager extends RecyclerView.Adapter<HomeManager.RecyclerViewHolder> {
    private static DisplayImageOptions displayOptions;
    static View v1;
    private SparseBooleanArray mCheckedItems = new SparseBooleanArray();

    static {
        displayOptions = DisplayImageOptions.createSimple();
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        CheckBox mCheck;

        RecyclerViewHolder(final View itemView) {
            super(itemView);

            mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
            SharedPreferences update = v1.getContext().getSharedPreferences("my_prefs", 0);
            String Check = update.getString("Check", "");
            Log.e("Checked Array = ", Check);

        }
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);

        return new RecyclerViewHolder(v1);

    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {

//      viewHolder.mCheck.setChecked(mCheckedItems);


        viewHolder.mCheck.setChecked(mCheckedItems.get(i));
        viewHolder.mCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item Favorited", Snackbar.LENGTH_SHORT);
                    snackbar.show();
                } else {
                    Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item Unfavorited", Snackbar.LENGTH_SHORT);
                    snackbar.show();
                }
                int position = viewHolder.getAdapterPosition();
                mCheckedItems.put(position, isChecked);
                List<Integer> selected = new ArrayList<>();
                for (int i = 0; i < mCheckedItems.size(); i++) {
                    final boolean checked = mCheckedItems.valueAt(i);
                    if (checked) {
                        selected.add(mCheckedItems.keyAt(i));
                    }
                }
                Log.e("Checked Array = ", String.valueOf(selected));
                SharedPreferences prefs = v1.getContext().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = prefs.edit();
                edit.putString("Checkbox", String.valueOf(selected));
                edit.commit();
                Log.e("Shared Preference = ", String.valueOf(edit));
            }
        });

}

2 个答案:

答案 0 :(得分:1)

问题很简单 - 您必须使用相同的密钥进行存储和访问。

您目前正在尝试使用“检查”键访问存储的数据:

SharedPreferences update = v1.getContext().getSharedPreferences("my_prefs", 0);
String Check = update.getString("Check", "");

但是您使用“Checkbox”键存储它:

SharedPreferences prefs = v1.getContext().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("Checkbox", String.valueOf(selected));
edit.commit();

两者都使用“Check”或“Checkbox”!你不能混搭。

答案 1 :(得分:1)

您已将共享首选项中的trh字符串设为Checkbox

 edit.putString("Checkbox", String.valueOf(selected));

但是你得到了名为Check

的字符串
SharedPreferences update = v1.getContext().getSharedPreferences("my_prefs",0);
    String Check = update.getString("Check", "");

将其更改为

 String Check = update.getString("Checkbox", "");