Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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 如何在RecyclerView适配器kotlin中获得第二个型号的位置_Android_Kotlin_Android Recyclerview_Android Adapter - Fatal编程技术网

Android 如何在RecyclerView适配器kotlin中获得第二个型号的位置

Android 如何在RecyclerView适配器kotlin中获得第二个型号的位置,android,kotlin,android-recyclerview,android-adapter,Android,Kotlin,Android Recyclerview,Android Adapter,这是我的RecyclerView适配器代码 'class RecyclerAdapterMain( val product: ArrayList, val viewmodel: ViewModelRoom, val newitem: List ) : RecyclerView.Adapter() { class ViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview) { val title: Text

这是我的RecyclerView适配器代码

'class RecyclerAdapterMain( val product: ArrayList, val viewmodel: ViewModelRoom, val newitem: List ) : RecyclerView.Adapter() { class ViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview) { val title: TextView = itemview.product_txt val price: TextView = itemview.price_product val imageproduct: ImageView = itemview.product_image val btn_add_product: Button = itemview.btn_add_product val amount_value: TextView = itemview.amount_value val button_remove_product: Button = itemview.button_remove_product } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val layoutview = LayoutInflater.from(parent.context).inflate(R.layout.product_items, parent, false) return ViewHolder(layoutview) } override fun getItemCount(): Int = product.size override fun onBindViewHolder(holder: ViewHolder, position: Int) { val products = product[position] holder.title.text = products.title holder.price.text = products.price.toString() Picasso.get().load(products.image).into(holder.imageproduct) holder.itemView.setOnClickListener { val bundle = Bundle() val myfragment = ItemDetailsfragment() myfragment.arguments = bundle val activity = it.context as AppCompatActivity activity.supportFragmentManager.beginTransaction() .replace(R.id.homepage, myfragment) .commit() bundle.putString("title", products.title) bundle.putString("price", products.price.toString()) bundle.putString("image", products.image.toString()) } holder.amount_value.visibility = View.GONE holder.button_remove_product.visibility = View.GONE holder.btn_add_product.setOnClickListener { holder.amount_value.visibility = View.VISIBLE holder.button_remove_product.visibility = View.VISIBLE holder.amount_value++ } } “分类回收商Dapterman( val产品:ArrayList, val viewmodel:ViewModelRoom, val newitem:列表 ) : RecyclerView.Adapter(){ 类ViewHolder(itemview:View):RecyclerView.ViewHolder(itemview){ val标题:TextView=itemview.product_txt val价格:TextView=itemview.price\u产品 val imageproduct:ImageView=itemview.product\u图像 val btn_add_product:Button=itemview.btn_add_product val金额\值:TextView=itemview.amount\值 val button_remove_product:button=itemview.button_remove_product } override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{ val布局视图= LayoutFlater.from(parent.context)。充气(R.layout.product\u项,parent,false) 返回视图保持架(布局视图) } 重写getItemCount():Int=product.size 覆盖BindViewHolder(holder:ViewHolder,位置:Int){ val products=产品[职位] holder.title.text=产品.title holder.price.text=products.price.toString() Picasso.get().load(products.image).into(holder.imageproduct) holder.itemView.setOnClickListener{ val bundle=bundle() val myfragment=ItemDetailsfragment() myfragment.arguments=bundle val activity=it.context作为AppCompatActivity activity.supportFragmentManager.beginTransaction() .replace(R.id.homepage,myfragment) .commit() bundle.putString(“title”,products.title) bundle.putString(“price”,products.price.toString()) bundle.putString(“image”,products.image.toString()) } holder.amount\u value.visibility=View.GONE holder.button\u remove\u product.visibility=View.GONE holder.btn\u add\u product.setOnClickListener{ holder.amount\u value.visibility=View.visibility holder.button\u remove\u product.visibility=View.VISIBLE 持有人.金额_值++ } } 问题是我希望当按下按钮时,产品数量会增加…为此,我需要每个项目的位置,但我的产品的第一个型号(override fun getItemCount():Int=product.size)获得了我需要的新型号位置(val newitem:List)


我尝试了这个代码val productpos=newitem.position,并给我一个错误,大小为1,索引也为1…我不知道如何修复它。刚才我需要一个新模型的位置。如何获得一个新位置?

这回答了你的问题吗?这回答了你的问题吗?