我的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();}
}
});
}
提前感谢。
答案 0 :(得分:2)
假设viewHolder.mCheck
是CheckedTextView
,我会使用以下内容:
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
来存储检查哪些元素。