Hi I am using Recyclerview with Adapter in my Android application. In my app I have product listing screen, where user can set the quantity in every list item. I am using two imageviews for increment and decrement the quantity. Now issue is , if I make 2 quantity for first product and 4 for second product, total count should be 6 for my cart screen, but I am not able to get total count. Following is my code for adapter, can anyone help me ?
class ProductAdapter(private val cellClickListener: CellClickListener) : RecyclerView.Adapter<ProductViewHolder>() {
var productsList = mutableListOf<Product>()
var cartList = mutableListOf<Product>()
/* fun setMovieList(movies: List<MobileList>) {
this.movies = movies.toMutableList()
notifyDataSetChanged()
}*/
fun addData(data:List<Product>){
productsList.addAll(data)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = AdapterLayoutBinding.inflate(inflater, parent, false)
return ProductViewHolder(binding)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val products = productsList[position]
holder.binding.name.text = products.name
holder.binding.price.text = products.price
Glide.with(holder.itemView.context).load(products.image_url).into(holder.binding.imageview)
var cartCount : Int=0
holder.binding.cartPlus.setOnClickListener {
cartCount
holder.binding.cartValue.text = cartCount.toString()
cartList.add(products)
//println(cartList)
}
holder.binding.cartMinus.setOnClickListener {
cartCount--
holder.binding.cartValue.text = cartCount.toString()
cartList.remove(products)
// println(cartList)
}
fun updatedData( updatedcartList:List<Product>){
cartList.addAll(updatedcartList)
}
holder.itemView.setOnClickListener {
cellClickListener.onCellClickListener(products,cartCount)
}
}
override fun getItemCount(): Int {
return productsList.size
}
}
class ProductViewHolder(val binding: AdapterLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
}
interface CellClickListener {
fun onCellClickListener(data: Product,count:Int)
}
CodePudding user response:
use cartlist.size()
for total count.
and for using in activity define this in Adapter class:
class ProductAdapter(private val..
{
...
fun getCartSize():Int {
return cartlist.size()
}
...
}
and in the activity you can use :
adapter.getCartsize()