首页 > 解决方案 > 从适配器中移除的项目未重新排列

问题描述

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

我有一个项目数组(项目 A、项目 B),我可以向这些项目添加数量,所以我创建了第二个数组来存储项目名称、项目数量和项目价格,我这样做是为了有一个带有我选择的物品的基本购物车。

因此,当我按下项目 A 上的 + 按钮数量增加时,与项目 B 相同,这可以正常工作,但现在当我单击减号按钮时 - 数量为 1,我从 selecteditem 数组中删除该项目,所以现在我有数量为 3 的项目 b,但是由于我从该数组中删除了项目 a,现在项目 B 位于位置 0,这意味着当我按项目 A 更新其值时,它将更新项目的值B 和项目 A 将创建在位置 2,项目 b 所在的位置,所以当我更新项目位将更新项目 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 上的产品,但会保留对 productlist 索引的引用,所以当更新任何其他项目的数量时,所有索引都发生了变化并且更新是错误的

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)

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

谢谢

标签: androidkotlinandroid-recyclerviewandroid-adapter

解决方案


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

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

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

创建一个方法,循环遍历 ProductCartSelected 对象列表并返回您实际修改的一个对象让我们在 OnBindViewHolder 中调用此方法fun ProductCartSelected findCartObject(cartId: Int)(遗憾的是我不会说 kotlin),而不是将位置传递给updateProductQuantity您将通过的方法它是一个购物车 ID(或者是产品名称),并且在内部,此方法将使用 findCartObject 方法来返回应该实际修改的正确对象。


推荐阅读