У меня есть recyclerview
с checkbox
, который будет добавлять элемент к Arraylist
каждый раз, когда проверяется checkbox
.Но я хочу, чтобы они удалили элемент, когда checkbox
снят
public class AttendeesRecAdapter extends RecyclerView.Adapter<AttendeesRecAdapter.ViewHolder>{
private Context context;
private AttendeesListener attendeesListener;
private ArrayList<AttendeesDUmmy> dumme;
ArrayList<AttendDummyAgain> list = new ArrayList<>();
public AttendeesRecAdapter(ArrayList<AttendeesDUmmy> dumme,Context context,AttendeesListener attendeesListener) {
this.context = context;
this.dumme = dumme;
this.attendeesListener = attendeesListener;
}
public interface AttendeesListener{
void onClickAtLayout(AttendeesDUmmy attt);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attendees_card,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(AttendeesRecAdapter.ViewHolder holder, int position) {
AttendeesDUmmy att = dumme.get(position);
holder.txName.setText(att.getName());
holder.txtNRIC.setText(att.getNRIC());
holder.txtPassport.setText(att.getPassport());
holder.txtmV.setText(att.getWp());
holder.cardItemRoot.setOnClickListener(v -> attendeesListener.onClickAtLayout(att));
holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked){
list.add(new AttendDummyAgain(att.getName(),att.getWp()));
Logger.d(" Attende Dummy" +list);
notifyDataSetChanged();
}else {
Logger.d(" Attende Dummy" +list);
if (list.size() != 0){
list.remove(position);
notifyItemRemoved(position);
Logger.d(" Attende Dummy" +list);
}
}
});
}
@Override
public int getItemCount() {
if (dumme != null){
return dumme.size();
}else {
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView txName;
TextView txtmV;
TextView txtPassport;
TextView txtNRIC;
CardView cardItemRoot;
CheckBox checkBox;
public ViewHolder(View itemView) {
super(itemView);
txName = itemView.findViewById(R.id.attendees_name);
txtNRIC = itemView.findViewById(R.id.attendees_NRIC);
txtPassport = itemView.findViewById(R.id.attendees_passport);
txtmV = itemView.findViewById(R.id.attendees_wp);
cardItemRoot = itemView.findViewById(R.id.attendees_card_item_root);
checkBox = itemView.findViewById(R.id.attendees_checkboxes);
}
}
}