Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

android - How to get the total count in cart using Recyclerview Adapter

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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...