我有一个申请, 想法很简单,用户可以选择几个选项进行投票,并显示投票的百分比。
在Kotlin中,我有一个RecyclerView,其中包含几个按钮和一个TextView。他们做了一些计算。问题在于,当移至回收站视图中的另一个项目时,结果将继续超过最后一个项目中的相同结果。我希望每个项目都是独立的,并且不要干扰其他项目中的其他结果。 我在处理商品位置时遇到问题。
package com.example.dell.vote1
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.support.v4.content.ContextCompat.startActivity
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import com.example.dell.vote1.R.id.*
import com.example.dell.vote1.R.layout.main_vote
import com.example.dell.vote1.R.layout.raw_re2
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
class CustomAdapter(val voteList: ArrayList<VotePublic>)
:RecyclerView.Adapter<CustomAdapter.ViewHolder> () {
var numofAllvotes : Float = 0f
var numofvoter1 : Float = 0f
var numofvoter2 : Float = 0f
var numofvoter3 : Float = 0f
var numofvoter4 : Float = 0f
@SuppressLint("ResourceAsColor")
override fun onBindViewHolder(holder: ViewHolder, p1: Int) {
val votePublic:VotePublic = voteList[p1]
holder.quesVote1.text = votePublic.quesVote
holder.tv_vote1.text = votePublic.vote11
holder.tv_vote2.text = votePublic.vote22
holder.tv_vote3.text = votePublic.vote33
holder.tv_vote4.text = votePublic.vote44
fun vote1Click () {
val percentage2 : Float
val percentage3 : Float
val percentage4 : Float
val percentage1: Float
this.numofvoter1++
this.numofAllvotes++
percentage1 = (this.numofvoter1 / this.numofAllvotes) * 100
holder.percentage1.text = percentage1.toInt().toString()
holder.textViewvotesNum.text = this.numofAllvotes.toInt().toString()
percentage2 = (this.numofvoter2 / this.numofAllvotes) * 100
holder.percentage2.text = percentage2.toInt().toString()
percentage3 = (this.numofvoter3 / this.numofAllvotes) * 100
holder.percentage3.text = percentage3.toInt().toString()
percentage4 = (this.numofvoter4 / this.numofAllvotes) * 100
holder.percentage4.text = percentage4.toInt().toString()
}
fun vote2Click () {
val percentage1 : Float
val percentage3 : Float
val percentage4 : Float
val percentage2: Float
this.numofvoter2++
this.numofAllvotes++
percentage2 = (this.numofvoter2 / this.numofAllvotes) * 100
holder.percentage2.text = percentage2.toInt().toString()
holder.textViewvotesNum.text = this.numofAllvotes.toInt().toString()
percentage1 = (this.numofvoter1 / this.numofAllvotes) * 100
holder.percentage1.text = percentage1.toInt().toString()
percentage3 = (this.numofvoter3 / this.numofAllvotes) * 100
holder.percentage3.text = percentage3.toInt().toString()
percentage4 = (this.numofvoter4 / this.numofAllvotes) * 100
holder.percentage4.text = percentage4.toInt().toString()
}
fun vote3Click () {
val percentage1 : Float
val percentage2 : Float
val percentage4 : Float
val percentage3: Float
this.numofvoter3++
this.numofAllvotes++
percentage3 = (this.numofvoter3 / this.numofAllvotes) * 100
holder.percentage3.text = percentage3.toInt().toString()
holder.textViewvotesNum.text = this.numofAllvotes.toInt().toString()
percentage1 = (this.numofvoter1 / this.numofAllvotes) * 100
holder.percentage1.text = percentage1.toInt().toString()
percentage2 = (this.numofvoter2 / this.numofAllvotes) * 100
holder.percentage2.text = percentage2.toInt().toString()
percentage4 = (this.numofvoter4 / this.numofAllvotes) * 100
holder.percentage4.text = percentage4.toInt().toString()
}
fun vote4Click () {
val percentage1 : Float
val percentage2 : Float
val percentage3 : Float
val percentage4: Float
this.numofvoter4++
this.numofAllvotes++
percentage4 = (this.numofvoter4 / this.numofAllvotes) * 100
holder.percentage4.text = percentage4.toInt().toString()
holder.textViewvotesNum.text = this.numofAllvotes.toInt().toString()
percentage1 = (this.numofvoter1 / this.numofAllvotes) * 100
holder.percentage1.text = percentage1.toInt().toString()
percentage2 = (this.numofvoter2 / this.numofAllvotes) * 100
holder.percentage2.text = percentage2.toInt().toString()
percentage3 = (this.numofvoter3 / this.numofAllvotes) * 100
holder.percentage3.text = percentage3.toInt().toString()
}
holder.tv_vote1.setOnClickListener {
vote1Click()
}
holder.tv_vote2.setOnClickListener {
vote2Click()
}
holder.tv_vote3.setOnClickListener {
vote3Click()
}
holder.tv_vote4.setOnClickListener {
vote4Click()
}
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val v = LayoutInflater.from(p0.context).inflate(R.layout.raw_re2,p0,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return voteList.size
}
class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val quesVote1 = itemView.findViewById(R.id.quesVote1) as TextView
val tv_vote1 = itemView.findViewById(R.id.tv_vote1) as TextView
val tv_vote2 = itemView.findViewById(R.id.tv_vote2) as TextView
val tv_vote3 = itemView.findViewById(R.id.tv_vote3) as TextView
val tv_vote4 = itemView.findViewById(R.id.tv_vote4) as TextView
val percentage1 = itemView.findViewById(R.id.percentage1) as TextView
val percentage2 = itemView.findViewById(R.id.percentage2) as TextView
val percentage3 = itemView.findViewById(R.id.percentage3) as TextView
val percentage4 = itemView.findViewById(R.id.percentage4) as TextView
val textViewvotesNum = itemView.findViewById(R.id.textViewvotesNum) as TextView
}
}