Search code examples
androidandroid-sharedpreferences

Checkbox SharedPreference Not working in Recyclerview Checkbox


I was trying to implement SharedPreference for storing checkbox checked list in my RecyclerView adapter. I am able to Log checked list arrays correctly. but I am not able to save it to shared preference and restore it when app reopens every time.

Here is my RecyclerView adapter code.

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

}

Solution

  • The problem is simple - you have to store and access with the same key.

    You are currently trying to access your stored data with the key "Check":

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

    But you're storing it with the key "Checkbox":

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

    Either use "Check" or "Checkbox" for both! You can't mix and match.