Android 我想计算所有项目的总分,并显示项目详细信息

Android 我想计算所有项目的总分,并显示项目详细信息,android,kotlin,Android,Kotlin,//这是我的个人列表A,我想在列表的每个项目中显示所有项目的总分 //我试过了,但分数总是显示为0.0,好像不算在内 //我尝试在开始时初始化所有内容,但得到了相同的结果 class PersonListAdapter( private var context: Context, private var onItemClickListener: OnItemClickListener ) : RecyclerView.Adapter<Pers

//这是我的个人列表A,我想在列表的每个项目中显示所有项目的总分 //我试过了,但分数总是显示为0.0,好像不算在内 //我尝试在开始时初始化所有内容,但得到了相同的结果

    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()
    
               
            }
        }
    }
类PersonListAdapter(
私有变量上下文:上下文,
私有变量onItemClickListener:onItemClickListener
):RecyclerView.Adapter(){
//这是人员名单
private var mArrayList:ArrayList=ArrayList()
var总计:双倍=0.0
//这是我列出清单的地方
乐趣列表(mArrayList:ArrayList){
这个.mArrayList.clear()
this.mArrayList=mArrayList
notifyDataSetChanged()
}
//我尝试了这个初始化,因为总数始终为0.0,并且列表包含某些项
初始化{
mArrayList.forEach{item->
总计+=项目.得分?.toDouble()!!
}
}
//我试过了,但分数总是显示为0.0,好像不算在内
//我在开始时尝试初始化所有内容,但得到了相同的结果
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
持有人绑定(结婚名单[职位])
}
//我试过了,但分数总是显示为0.0,好像不算在内
内部类ViewHolder(变量视图:视图):RecyclerView.ViewHolder(视图){
趣味绑定(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()
}
}
}

可以尝试将总数保存到个人:

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


init
在构建类时运行,并且您已经用

private var mArrayList: ArrayList<Person> = ArrayList()
private var mArrayList: ArrayList<Person> = ArrayList()
total = mArrayList.sumBy { it.score ?: 0 }
// you've said it's nullable so defaulting with 0 in that case