android - 添加 RecyclerView 项目总计
问题描述
在我的项目中,我有一个包含适配器和片段类的回收器视图。一切都按预期工作,我正在尝试对特定列值求和并将其显示在文本字段中。我尝试了以下总方法,但它不起作用。任何帮助表示赞赏。
使用的语言
Kotlin
使用的代码
适配器类
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()
}
分段
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()
}
期待
Total CarPrice in the Recycler View.
解决方案
你可以使用 Kotlin List 的 sumBy 方法来做到这一点。
在您的片段类中添加以下代码
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()
}
推荐阅读
- c# - 在 .Net Core 类库中添加启动类
- networkx - 在示例代码上使用 Networkx 的 RuntimeError
- java - Apache Beam 如何将 TestStream 与文件一起使用
- javascript - 初学者关于JS中递归示例的问题
- excel - Excel/电子表格:如何计算日期之间的计数
- database - 在 kubernetes pod 上运行高可用数据库(使用自动配置)
- powerbi - 如何在矩阵中重复行标签?
- xamarin - 在 Xamarin 表单 TabbedPage 中添加更多选项卡
- java - 将上下文传递给非静态方法 - 我仍然无法做到这一点
- javascript - 为什么 html 表单提交按钮不能在 android 手机上工作但在 pc 上工作?