Get list of checked checkboxes from recyclerview android
One simple solution is to create OnItemCheckLister
, and implement it similar to following:
public class MyAdapter extends RecyclerViewAdapter {
interface OnItemCheckListener {
void onItemCheck(Item item);
void onItemUncheck(Item item);
}
...
@NonNull
private OnItemCheckListener onItemClick;
public MyAdapter (List<Item> items, @NonNull OnItemCheckListener onItemCheckListener) {
this.items = items;
this.onItemClick = onItemCheckListener;
}
...
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
final Item currentItem = items.get(position);
...
((MyViewHolder) holder).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MyViewHolder) holder).checkbox.setChecked(
!((MyViewHolder) holder).checkbox.isChecked());
if (((MyViewHolder) holder).checkbox.isChecked()) {
onItemClick.onItemCheck(currentItem);
} else {
onItemClick.onItemUncheck(currentItem);
}
}
});
}
}
static class MyViewHolder extends RecyclerView.ViewHolder {
CheckBox checkbox;
View itemView;
...
public MyViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
checkbox.setClickable(false);
...
}
public void setOnClickListener(View.OnClickListener onClickListener) {
itemView.setOnClickListener(onClickListener);
}
}
}
Then in the activity, we can do this:
private List<Item> currentSelectedItems = new ArrayList<>();
...
myAdapter = new MyAdapter(items, new MyAdapter.OnItemCheckListener() {
@Override
public void onItemCheck(Item item) {
currentSelectedItems.add(item);
}
@Override
public void onItemUncheck(Item item) {
currentSelectedItems.remove(item);
}
});
Then you can do stuff with currentSelectedItems
.
Note: Since this solution suppose for the whole Item to be pressed so the checkbox is set to be not clickable.
You can use Model class to get recyclerView checked items. More reference : Android Recyclerview checkbox
Model
public class Model {
private boolean isSelected;
private String animal;
public String getAnimal() {
return animal;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public boolean getSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
Adapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static ArrayList<Model> imageModelArrayList;
private Context ctx;
public CustomAdapter(Context ctx, ArrayList<Model> imageModelArrayList) {
inflater = LayoutInflater.from(ctx);
this.imageModelArrayList = imageModelArrayList;
this.ctx = ctx;
}
@Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
holder.checkBox.setText("Checkbox " + position);
holder.checkBox.setChecked(imageModelArrayList.get(position).getSelected());
holder.tvAnimal.setText(imageModelArrayList.get(position).getAnimal());
// holder.checkBox.setTag(R.integer.btnplusview, convertView);
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer pos = (Integer) holder.checkBox.getTag();
Toast.makeText(ctx, imageModelArrayList.get(pos).getAnimal() + " clicked!", Toast.LENGTH_SHORT).show();
if (imageModelArrayList.get(pos).getSelected()) {
imageModelArrayList.get(pos).setSelected(false);
} else {
imageModelArrayList.get(pos).setSelected(true);
}
}
});
}
@Override
public int getItemCount() {
return imageModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
protected CheckBox checkBox;
private TextView tvAnimal;
public MyViewHolder(View itemView) {
super(itemView);
checkBox = (CheckBox) itemView.findViewById(R.id.cb);
tvAnimal = (TextView) itemView.findViewById(R.id.animal);
}
}