Search code examples
androidandroid-recyclerview

android Recyclerview removing item does not update adapter's get item count


I have a RecyclerView with swiping feature to reveal a delete and edit button.

I added: adapter.notifyItemRemoved(position) and this: adapter.notifyItemRangeChanged(0, adapter.getItemCount());

when the revealed delete button is clicked, the animation for removing the item works and The item is deleted from my database

BUT then the deleted item re-appears in my recyclerview. When I change activity and go back, to the activity with the recyclerview, the list that I should be seeing is good.

If I remove the "notifyItemRangeChanged" code, the list updates with the last item repeated.

I think it is my Adapter's getItemCount not properly updating. so what I tried differently was to call my method that generates the list in the first place. This did the trick BUT my remove item animation is gone now because I guess it just skips to re-generate the list....

Any ideas?

Thank you in advanced for your feedback!

****************** UPDATE - ADDING ADAPTER CLASS CODE ****************
public class RVCategoryAdapter extends RecyclerView.Adapter<CategoryViewHolder> {
        Context context;
        List<CategoryItem> categoryItemList;
    
        public RVCategoryAdapter(Context context, List<CategoryItem> categoryItemList) {
            this.context = context;
            this.categoryItemList = categoryItemList;
        }
    
        @NonNull
        @Override
        public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.category_item_layout, parent, false);
    
            return new CategoryViewHolder(itemView);
        }
    
        @Override
        public void onBindViewHolder(@NonNull CategoryViewHolder holder, final int position) {
            final int categoryID;
            final String categoryTitle;
    
            Glide.with(context).load(categoryItemList.get(position).getImage()).into(holder.ivCategoryIcon);
            holder.txtCatID.setText(""+categoryItemList.get(position).getCategoryID());
            holder.txtCategoryTitle.setText(categoryItemList.get(position).getTitle());
            holder.txtCategoryDesc.setText(categoryItemList.get(position).getDescription());
    
            categoryID = Integer.parseInt(holder.txtCatID.getText().toString());
            categoryTitle = holder.txtCategoryTitle.getText().toString();
            holder.cardViewItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, NotesListActivity.class);
                    intent.putExtra("CategoryID", categoryID);
                    intent.putExtra("CategoryTitle", categoryTitle);
                    context.startActivity(intent);
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return categoryItemList.size();
        }
    }

Solution

  • In your swipe delete button click listener remove your item from the list, too. I would suggest you to add delete function in your adapter. Then in that method delete your item from list and call notifyItemRemoved.

    public void delete(int position){
        categoryItemList.remove(position);
        notifyItemRemoved(position);
    }