从不重新排列的适配器中删除项目

Removed item from Adapter not rearanged

我正在制作一个可以将一堆商品添加到购物车的应用程序,现在,我有这个 UI 可以使用 + 按钮添加一个商品,如果我按 - 则将其从数组中删除数量为1时的按钮。

我有一个项目数组(项目 A、项目 B),我可以向这些项目添加数量,所以我创建了第二个数组来存储项目名称、项目数量和项目价格,我执行此操作以获得包含我选择的项目的基本购物车。

因此,当我按 + 按钮增加我的项目 A 的数量时,与项目 B 一样,这正常工作,但现在当我单击减号按钮时 - 数量为 1,我删除了该项目从 selecteditem 数组中,所以现在我有数量为 3 的项目 b 但由于我从该数组中删除了项目 a,现在项目 B 位于位置 0,这意味着当我按项目 A 更新其值时,它将是更新项目 B 和项目 A 的值将在项目 b 所在的位置 2 处创建,因此当我更新项目位时将更新项目 A 并且这将继续

这是我用来更新项目值的代码

适配器

var productList = mutableListOf<Product>()
 var cartProductList = mutableListOf<ProductCartSelected>()


 inner class StoreViewHolder(itemView: View): BaseViewHolder<Product>(itemView){
        override fun bind(item: Product,position: Int) {
            //First button click before select quantity
            itemView.btn_action_add.setOnClickListener {
                itemView.btn_action_add.visibility = View.GONE
                itemView.card_selector_product.visibility = View.VISIBLE
                itemClickListener?.onCartClick(item)
            }

            itemView.btn_increase.setOnClickListener {
                val currentQuantity = itemView.txt_quantity.text.toString().toInt()
                if(currentQuantity != 20){
                    val quantity = currentQuantity + 1
                    display(quantity)
                    updateProductQuantity(position,quantity)
                }
            }

            itemView.btn_decrease.setOnClickListener {
                val currentQuantity = itemView.txt_quantity.text.toString().toInt()
                if(currentQuantity == 1){
                    removeSelectedProduct(position)
                }else{
                    val quantity = currentQuantity - 1
                    display(quantity)
                    updateProductQuantity(position,quantity)
                }
            }

        }

  fun setItems(selectedProductList: MutableList<Producto>) {
        productList = selectedProductList
        notifyDataSetChanged()
    }

    fun setProductSelected(productCartSelected: ProductCartSelected){
        cartProductList.add(productCartSelected)
    }

    fun removeSelectedProduct(position: Int){
        cartProductList.removeAt(position)
        notifyDataSetChanged()
    }

    fun updateProductQuantity(position: Int,quantity:Int){
        cartProductList[position].quantity = quantity
        itemClickListener?.onUpdatedProductQuantity(cartProductList[position])
    }

所以,基本上问题出在我使用 removeSelectedProduct(position) 时,因为它会删除 cartProductList 上的产品,但会保留对产品列表索引的引用,因此在更新任何其他项目的数量时,所有索引都会更改并且更新有误

在我看来,我用 setItems() 方法发送了项目 a 和项目 B

数据类

@Parcelize
data class Product(
    val name:String = "", val price:Int = 0,val productImage:String = "",val hasDiscount:Boolean = false
) : Parcelable

data class ProductCartSelected(var quantity:Int,var productName: String,var price: Int)

如果需要任何其他信息,请问我

谢谢

我在 java 中编程,但我遇到了同样的问题。我建议您创建一个用于填充 recyclerview 的列表。而不是仅仅将产品名称传递给您的 ProductCartSelected class 为什么不传递产品本身呢?为了更好的衡量,我也会给它一个 ID。

因为您将只使用一个列表,所以这应该可以解决您的问题,因为您的 recyclerview 中的位置和您的 ProductCartSelected 列表应该保持同步。

或者创建一个方法,使用类似于我建议的 ID 来检查您正在修改的项目的正确位置,然后再修改它们。但我会先用以前的方法试试

创建一个循环遍历 ProductCartSelected 对象列表的方法,return让您实际修改的一个对象调用此方法 fun ProductCartSelected findCartObject(cartId: Int)(遗憾的是我不会说 kotlin)在你的 OnBindViewHolder 中,而不是将位置传递给 updateProductQuantity 等方法,你将传递一个 cartID(或者一个 productName),并且在内部这个方法将使用 findCartObject 方法来 return 正确的对象实际上应该修改。