In my Project, I have a recyclerview which contains adapter and fragment class. Everything is working as expected and I am trying to sum a particular column value and display it in a text field. I tried below total method but its not working. Any help is appreciated.
Language Used
Kotlin
Code Used
Adapter Class
override fun onBindViewHolder(holder: ViewHolder, position: Int)
{
holder.bindcar(list[position],fragment)
}
fun bindcar(Test: TestCart?, fragment: TestFragment)=with(carView)
{
TestName.text=Test?.carName
TestQuantity.text= Test?.carQuantity.toString()
TestPrice.text= Test?.carPrice!!.toString()
}
Fragment
for (c in carList!!.iterator())
{
val car=TestCart()
//var carPrice=0
car.carName=c.carName
car.carQuantity=c.carQuantity
car.carPrice=c.carPrice
// car.carPrice= c.carPrice!! +Price
carListcars!!.add(car)
}
adapter!!.notifyDataSetChanged()
}
Expectation
Total CarPrice in the Recycler View.
you can use sumBy method of Kotlin List to do that.
Inside your fragment class add below code
for (c in carList!!.iterator())
{
val car=TestCart()
//var carPrice=0
car.carName=c.carName
car.carQuantity=c.carQuantity
car.carPrice=c.carPrice
// car.carPrice= c.carPrice!! +Price
carListcars!!.add(car)
}
adapter!!.notifyDataSetChanged()
// sumby code
var totalAmount: Int = carListcars.sumBy{ it.carPrice }
// setting your textView
TestPrice.text = totalAmount.toString()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.