Skip to content Skip to sidebar Skip to footer

Dynamically Deleteing Cardviews But Error

I have this code so far when the user presses the fab a cardview is added to the recyclerview. I also now made it so whenthe user presses a specific cardview, a dialog shows and wh

Solution 1:

The position of an item inside your list may change while you add and remove items from it. So, the position you assigned in your onClickListener may not be the right one in the moment of the click.

Try with this:

publicvoidonClick(DialogInterface dialog, int id) {
    // ...

    productList.remove(holder.getAdapterPosition());
    notifyItemRemoved(holder.getAdapterPosition());

    // ...
}

This way, you will be checking the position at the time of the click, not when you bind the ViewHolder.

Post a Comment for "Dynamically Deleteing Cardviews But Error"