dimanche 26 septembre 2021

How to delete an item from recyclerview, in adapter class, when the checkbox in the itemview is checked [Android Studio]?

I have a tasks app where a checkbox is attached to the item. I want the item in recyclerview to be deleted or set invisible when the user checks the checkbox. But how do i delete an item in the adapter class?

My code so far -

static class TaskHolder extends RecyclerView.ViewHolder {
    private final TextView a_tname;
    private final TextView a_tdate;
    private  final TextView a_ttime;
    private final TextView a_tprior;
    ImageView priorityIndicator;
    CheckBox checkbox;

    public TaskHolder(View itemView) {
        super(itemView);
        a_tname = itemView.findViewById(R.id.a_tname);
        a_tdate=itemView.findViewById(R.id.a_tdate);
        a_ttime = itemView.findViewById(R.id.a_ttime);
        a_tprior = itemView.findViewById(R.id.a_tprior);
        priorityIndicator = itemView.findViewById(R.id.priorityIndicator);
        checkbox = itemView.findViewById(R.id.checkbox);
        checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkbox.isChecked()){
                    //how should i delete the item here?
                }
            }
        });

PS: I know how to delete it in the MainActivity through viewmodel and getAdapterPosition() but if I do the same thing in my adapter class, it shows NullPointerException. I am using android studio and Room database.




Aucun commentaire:

Enregistrer un commentaire