Reputation: 69
This my RecyclerView
code and I want increase the amount of a item product when the button plus is clicked.
class RecyclerAdapterMain(
val product: ArrayList<ModelProductMain>
) :
RecyclerView.Adapter<RecyclerAdapterMain.ViewHolder>() {
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 additem: Button = itemview.btn_icon_add
val rl_add = itemview.rl_section_additive_items
val amount: TextView = itemview.amount_value
val btnadd: Button = itemview.button_add_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() = 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.amount.text = products.amount.toString()
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.rl_add.visibility = View.GONE
holder.additem.setOnClickListener {
holder.rl_add.visibility = View.VISIBLE
holder.additem.visibility = View.GONE
}
holder.btnadd.setOnClickListener {
products.amount++
}
}
}
If I want to say what exactly I want from this code is that : when I click on the plus button the amount of product item increased. I try so many but cant increase the amount of product. How can I fix it ?
Upvotes: 1
Views: 99
Reputation: 2719
products.amount++
notifyDataSetChanged(bindingAdapterPosition)
Upvotes: 1