从 Recyclerview 保存复选框状态

Saving checkbox state from Recyclerview

我的 Recyclerview 中有复选框,现在我可以单击它并显示一个快餐栏。但是谁能帮我从 recyclerview 中保存复选框的状态。

我在 recyclerview 适配器的 onBindViewHolder 中创建了一个 onclicklistener。

 private SparseBooleanArray mCheckedItems = new SparseBooleanArray();
        @Override
        public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
            // get the single element from the main array
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.mirlogo)
                    .showImageForEmptyUri(R.drawable.gallery_no_image)
                    .showImageOnFail(R.drawable.gallery_image_fail)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();

            final String images = HomeDataImages.IMAGES[i];
            // Set the values

            ImageLoader.getInstance()
                    .displayImage(images, viewHolder.mImage, options);

             viewHolder.mCheck.setChecked(mCheckedItems.get(i));
        Log.d("Position=", String.valueOf((mCheckedItems.get(i))));

        viewHolder.mCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                int position = viewHolder.getAdapterPosition();
                mCheckedItems.put(position, isChecked);
                Log.d("Checked Array = ", String.valueOf(mCheckedItems));
//                viewHolder.mCheck.setChecked(mCheckedItems);
//error comes here setChecked(boolean) cannot be applied to SparseBooleanArray
               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();}
               }
            });

    }

提前致谢。

假设 viewHolder.mCheckCheckedTextView 我会使用这样的一些:

private SparseBooleanArray mCheckedItems = new SparseBooleanArray();

@Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
    // get the single element from the main array
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.mirlogo)
            .showImageForEmptyUri(R.drawable.gallery_no_image)
            .showImageOnFail(R.drawable.gallery_image_fail)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    final String images = HomeDataImages.IMAGES[i];
    // Set the values

    ImageLoader.getInstance()
            .displayImage(images, viewHolder.mImage, options);

    viewHolder.mCheck.setChecked(mCheckedItems.get(i));

    viewHolder.mCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = viewHolder.getAdapterPosition();
            final boolean newValue = !mCheck.isChecked();

            mCheckedItems.put(position, newValue);
            checkedTextView.setChecked(newValue);

            //display the text accordingly with the newValue value
            Snackbar snackbar = Snackbar.make(v, "Item Favorited", Snackbar.LENGTH_SHORT);
            snackbar.show();
        }
    });

    public List<Integer> getSelectedItemPositions() {
        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));
            }
        }
        return selected;
    }

    public void restoreSelectedItems(List<Integer> positions){
        for (Integer position : positions) {
            mCheckedItems.put(position, true);
        }
    }
}

基本上我使用 SparseBooleanArray 来存储检查了哪些元素。