Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从适配器上拆下的项目未重新设置范围_Android_Kotlin_Android Recyclerview_Android Adapter - Fatal编程技术网

Android 从适配器上拆下的项目未重新设置范围

Android 从适配器上拆下的项目未重新设置范围,android,kotlin,android-recyclerview,android-adapter,Android,Kotlin,Android Recyclerview,Android Adapter,我正在制作一个应用程序,向购物车添加一堆物品,现在,我有一个UI,它使用+按钮添加一个物品,如果我在数量为1时按-按钮,它将从数组中删除 我有一个项目数组(项目A、项目B),我可以向这些项目添加数量,因此我创建了第二个数组来存储项目名称、项目数量和项目价格,我这样做是为了有一个包含我所选项目的基本购物车 因此,当我按下+按钮时,我的项目A上的数量增加,与项目B相同,这正常工作,但现在当我单击减号按钮时,数量为1,我将该项目从selecteditem数组中删除,因此现在我有了数量为3的项目B,但由

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

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

因此,当我按下+按钮时,我的项目A上的数量增加,与项目B相同,这正常工作,但现在当我单击减号按钮时,数量为1,我将该项目从selecteditem数组中删除,因此现在我有了数量为3的项目B,但由于我从该数组中删除了项目A,现在项目B位于位置0,这意味着,当我按下项目A更新其值时,它将更新项目B的值,项目A将在位置2创建,其中项目B位于位置2,因此当我更新项目bit时,它将更新项目A,这将继续

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

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


谢谢

我用java编程,但我也遇到了同样的问题。我建议您创建一个列表,用于填充您的recyclerview。与其将productname传递给您的
ProductCartSelected
类,为什么不将其传递给产品本身呢?为了更好的衡量,我也会给它一个身份证

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

或者,创建一个方法,使用类似于我建议的ID的内容,在修改项目之前检查它们的正确位置。但我会首先尝试使用前面的方法


创建一个方法,在ProductCartSelected对象列表中循环,并返回您实际修改的一个对象。让我们调用此方法
fun ProductCartSelected findCartObject(cartId:Int)
(正如我遗憾地说的,我不说kotlin)在OnBindViewHolder中,您将传递一个cartID(或者一个productName),而不是将位置传递给诸如
updateProductQuantity
之类的方法在内部,此方法将使用findCartObject方法,以返回实际应修改的正确对象。

因为传递第一个列表将有许多cart不会使用的属性,而传递永远不会使用的数据是一种不好的做法,相反,我只想传递我将在购物车上收到的数据,在这种情况下,请使用第二种方法。与其将位置传递给updateProductQuantity等方法,不如传递该ProductCartSelected对象特有的东西,如ID(但我猜productName也可以工作,前提是您不允许重复),我将在ID上添加一些代码听起来不错,就像在deletedhashmap中有一个对应于每个id的hashmap一样,如果您知道如何使用它,那么deletedhashmap会更好,只需创建ProductCartSelected对象的hashmap并使用它生成填充recyclerview的列表。之后,您只需使用该键进行所有修改。就我个人而言,我从来没有使用过Hashmap,但我已经考虑过了,所以让我知道它是如何进行的。
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])
    }
@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)