简体   繁体   中英

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 { Context context; List 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();
    }
}

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);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM