简体   繁体   English

正在添加RecyclerView项目总计

[英]Adding RecyclerView Item Total

In my Project, I have a recyclerview which contains adapter and fragment class. 在我的项目中,我有一个包含适配器和片段类的recyclerview。 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. 您可以使用Kotlin List的sumBy方法来做到这一点。

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


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM