skip to Main Content

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)
}

2

Answers


  1. 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()
    
    Login or Signup to reply.
  2. since you are just passing the list into the recycleview and want to use it in fragment or activity. I would suggest using the list to get the total count for your items instead. You can use sumOf to your list.

    you can use it like this before you pass to recyclver view.

    val totalCount = list.sumOf { it.quantity }
    

    or

     txtView.text = list.sumOf {it.quantity}.toString()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search