I want to calculate the total of scores of all items and show it the item details

Viewed 36

// this my PersonList Aadapter where I want to show in every item of the list the total of all scores of all the items // I tried this but the score always shows 0.0 as if it's not counting // I tried initializing the everything in the begin but I got the same result

    class PersonListAdapter(
        private var context: Context,
        private var onItemClickListener: OnItemClickListener
    ) : RecyclerView.Adapter<PersonListAdapter.ViewHolder>() {
    

// this is the list of persons

        private var mArrayList: ArrayList<Person> = ArrayList()
         var total : Double=0.0
    
    
// this where I set the list 
        fun setList(mArrayList: ArrayList<Person>) {
            this.mArrayList.clear()
            this.mArrayList = mArrayList
            notifyDataSetChanged()
        }
    
    
       
           
    
    //I tried this init by the total is always 0.0 and the list contains certain items 

        init {
    
            mArrayList.forEach {item->
    
                total += item.score?.toDouble()!!
    
            }
    
        }


  // I tried this  but the score always shows 0.0 as if it's not counting
                 // I tried initializing the everything in the begin but I got the same 
    
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.bind(mArrayList[position])
        }
    
                  // I tried this  but the score always shows 0.0 as if it's not counting
        inner class ViewHolder(var view: View) : RecyclerView.ViewHolder(view) {
            fun bind(personBean: Person) {
                itemView.tvPersonPhone.text = personBean.delai
                itemView.tvwiouw2.text = personBean.security
                                       var score= personBean.security.toString().toDouble()+personBean.delai.toString().toDouble()
    
                itemView.tvwiouw3.text = score.toString()



    
   
    
                   
    
                            itemView.tvwiouw4.text =total.toString()
    
               
            }
        }
    }
2 Answers

could try to save the total to Person:

init {
    mArrayList.forEach { item ->
        total += item.score?.toDouble()!!
        //must ensure total here is right
        item.total = total
    }
}

Then:

inner class ViewHolder(var view: View) : RecyclerView.ViewHolder(view) {
    fun bind(personBean: Person) {
        //...
        itemView.tvwiouw4.text = personBean.total.toString()
    }
}

init runs when the class is being constructed, and you've initialised your list with

private var mArrayList: ArrayList<Person> = ArrayList()

i.e. it's empty, so there's nothing to sum!

You need to calculate it whenever the list of items is set, so doing it in setList would probably be best.


Also there's a sumBy function if you want to use that, you give it a function to get a value for each item and it adds those up

total = mArrayList.sumBy { it.score ?: 0 }
// you've said it's nullable so defaulting with 0 in that case
Related