如何在Android中的RecyclerView中删除项目

4

我正在开发一个简单的购物车应用,它将在RecyclerView中显示购物车详情。 在这里,我将减少数量值到1。该商品将从商品适配器中删除。这个功能很好运行。 但是当我尝试减少第二个商品的数量值时,它会在没有检查条件的情况下被移除。 我的条件是如果数量低于1,则该商品将被删除。

ItemAdapter.java

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
    Context context;
    ArrayList<ItemData> itemDataList;
    int quantity,t_amount;
    totalAmount totalAmount;

    public ItemAdapter(Context context, ArrayList<ItemData> itemDataList) {
        this.context = context;
        this.itemDataList = itemDataList;
        totalAmount = (ItemAdapter.totalAmount) context;

    }

    @NonNull
    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_details,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int pos) {
        ItemData itemData = itemDataList.get(pos);
        holder.txtPrice.setText(itemData.getPrice());
        holder.txtDesc.setText(itemData.getDescription());
        holder.txtCartCount.setText(itemData.getCartCount());
        holder.btnCartDec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(quantity <= 1){
                    itemDataList.remove(holder.getAdapterPosition());
                    notifyItemRemoved(holder.getAdapterPosition());
                    notifyItemRangeChanged(holder.getAdapterPosition(),itemDataList.size());
                }else {
                    quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
                    quantity = quantity-1;
                    itemDataList.get(pos).setCartCount(String.valueOf(quantity));
                    notifyDataSetChanged();
                    holder.txtCartCount.setText(String.valueOf(quantity));
                }

            }
        });
        holder.btnCartInc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
                quantity = quantity+1;
                itemDataList.get(pos).setCartCount(String.valueOf(quantity));
                notifyDataSetChanged();
                holder.txtCartCount.setText(String.valueOf(quantity));

            }
        });
    }

    @Override
    public int getItemCount() {
        return itemDataList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtPrice, txtDesc, txtCartCount, btnCartDec, btnCartInc;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtPrice = itemView.findViewById(R.id.itemView_price);
            txtDesc = itemView.findViewById(R.id.itemView_desc);
            txtCartCount = itemView.findViewById(R.id.itemView_Count);
            btnCartDec = itemView.findViewById(R.id.itemViewDec);
            btnCartInc = itemView.findViewById(R.id.itemViewInc);
        }
    }
    public interface totalAmount{
        void t_amount(int amount);
    }

}
4个回答

5

只需要知道项目的位置,在适配器中添加以下代码即可

mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataSet.size());

0

在 if 条件之前,您必须获取与位置相关的 quantity。在您的情况下,quantity 的值是先前的值,这就是为什么 if 条件为真的原因。

holder.btnCartDec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // You must get the quantity of the currently clicked item here
                quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
                if(quantity <= 1){
                    itemDataList.remove(holder.getAdapterPosition());
                    notifyItemRemoved(holder.getAdapterPosition());
                    notifyItemRangeChanged(holder.getAdapterPosition(),itemDataList.size());
                }else {
                    quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
                    quantity = quantity-1;
                    itemDataList.get(pos).setCartCount(String.valueOf(quantity));
                    notifyDataSetChanged();
                    holder.txtCartCount.setText(String.valueOf(quantity));
                }

            }
        });

谢谢@sanoj,我的问题已经解决了。你的解决方案很有效。但我还想做一件事情,就是如何根据数量计算价格。每个项目都有不同的价格,如何获取所有项目价值的总和。 - amarlucky

0
你可以在特定位置从模型中移除你的项目。之后,使用notifyDataSetChanged();通知适配器列表中已经进行了一些更改。
list.remove(getAdapterPosition())
notifyDataSetChanged();

0

您可以从该位置的列表中删除并通知适配器,它将被删除。

替换此代码

itemDataList.remove(holder.getAdapterPosition());
                notifyItemRemoved(holder.getAdapterPosition());
                notifyItemRangeChanged(holder.getAdapterPosition(),itemDataList.size());

转换为这个

itemDataList.remove(holder.getAdapterPosition());notifyDataSetChanged();

它将会工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接