title: Checkbox SharedPreference Not working in Recyclerview Checkbox
body: <p>I was trying to implement <strong>SharedPreference for storing checkbox checked list in my <code>RecyclerView</code> adapter</strong>.
  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.</p>
  
  <p><em>Here is my <code>RecyclerView</code> adapter code.</em></p>
  
  <pre><code>public class HomeManager extends RecyclerView.Adapter&lt;HomeManager.RecyclerViewHolder&gt; {
      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&lt;Integer&gt; selected = new ArrayList&lt;&gt;();
                  for (int i = 0; i &lt; 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));
              }
          });
  
  }
  </code></pre>