I have a Recyclerview that contains a number of checkboxes. these checkboxes received from API(Checkboxes text and id). I want to get a list of ids of checked checkboxes to send to the server. One of my solutions is:
But my code is incorrect, when I checked a number of checkboxes. array list save the last checkboxes id? can you say another solution or fix this error?
Context context;
public ArrayList<Info_Filter> items = new ArrayList<>();
public SparseBooleanArray array = new SparseBooleanArray();
public ArrayList<Info_Checkbox> checkboxes = new ArrayList<>();
private Info_Checkbox info_checkbox = new Info_Checkbox();
public AdapterRecyFilterGroup(Context context, ArrayList<Info_Filter> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public SetViewHolderFilter onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.sample_filter_grouping, viewGroup, false);
return new SetViewHolderFilter(view);
}
@Override
public void onBindViewHolder(@NonNull SetViewHolderFilter setViewHolderFilter, int i) {
setViewHolderFilter.checkBox.setText(items.get(i).getName());
if (array.get(i)) {
setViewHolderFilter.checkBox.setChecked(true);
} else {
setViewHolderFilter.checkBox.setChecked(false);
}
}
@Override
public int getItemCount() {
return items == null ? 0 : items.size();
}
public class SetViewHolderFilter extends RecyclerView.ViewHolder {
CheckBox checkBox;
public SetViewHolderFilter(@NonNull final View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.cb_filter);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (array.get(getAdapterPosition())) { //!checked
array.put(getAdapterPosition(), false);
info_checkbox.setId(items.get(getAdapterPosition()).getId());
checkboxes.remove(info_checkbox);
} else { //checked
array.put(getAdapterPosition(), true);
info_checkbox.setId(items.get(getAdapterPosition()).getId());
checkboxes.add(info_checkbox);
}
notifyDataSetChanged();
}
});
}
}
you have to create a boolean variable in your Info_Filter class and when you are checking an checkbox then set the boolean variable value true and when you uncheck the box then change the boolean variable value false of that specific position, and after that when you need you can get the how many boolean variables are true and how many false then you can get the accurate items how many check or not.
This should do the trick:
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos < 0) {
pos = (int) checkBox.getTag();
}
boolean isChecked = !list.get(pos).isChecked;
items.get(pos).isChecked = isChecked;
if (isChecked) {
selectedCount++;
checkBox.setChecked(true);
} else {
selectedCount--;
checkBox.setChecked(false);
}
}
});
in BindViewHolder:
setViewHolderFilter.checkBtn.setChecked(list.get(position).isChecked);
Now you can get list checked checkBox in your items ArrayList and you can do anything.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.