0
私のコードのカウント星(5つ星)と私はそれがリアルタイムで更新された場合、それは正しくないKotlinでFirebase OnDataChangeの中に地図firebase で
(今Firebase)
- を=カウント、問題を抱えています1
- = 2
- = 0
- = 0
- = 0
および変更firebase後
- = 3
- = 4
- = 0
- = 0
- = 0
しかし、私はアプリを再起動した場合、それは正常な結果に変更します。
を- = 2
- = 2
- = 0
- = 0
- = 0
新しいスターにマップ秒の時間を追加MutableMapなぜ?あなたの例を使用して
class FireBaseRealTime {
var listofStarsCounter : ArrayList<Int> = ArrayList<Int>(listOf(0,0,0,0,0))
var mDataBase : DatabaseReference = FirebaseDatabase.getInstance().getReference()
var mDeviceLikesRef = mDataBase.child("service").child("devices").child("sam").child("likes")
fun allAverageLike(text: TextView,star1: TextView,star2: TextView,star3: TextView,star4: TextView,star5: TextView) {
var count : Int = 0
var sumAllvalue : Int = 0
mDeviceLikesRef.addValueEventListener(object : ValueEventListener{
override fun onCancelled(p0: DatabaseError?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onDataChange(dataSnapshot: DataSnapshot?) {
dataSnapshot?.children?.forEach { child : DataSnapshot ->
val objectMap : MutableMap<String, Any>
objectMap = child.value as MutableMap<String, Any>
for(entary in objectMap) {
if(entary.key.equals("value")) {
when(entary.value.toString()){
"1" -> listofStarsCounter.set(0, listofStarsCounter.get(0) + 1)
"2" -> listofStarsCounter.set(1, listofStarsCounter.get(1) + 1)
"3" -> listofStarsCounter.set(2, listofStarsCounter.get(2) + 1)
"4" -> listofStarsCounter.set(3, listofStarsCounter.get(3) + 1)
"5" -> listofStarsCounter.set(4, listofStarsCounter.get(4) + 1)
}
count++
sumAllvalue += entary.value.toString().toInt()
}
}
}
try {
text.text = "%.1f".format(sumAllvalue.toDouble()/count.toDouble())
star1.text = "%.1f".format((100.0/count.toDouble()) * listofStarsCounter.get(0).toDouble())
star2.text = "%.1f".format((100.0/count.toDouble()) * listofStarsCounter.get(1).toDouble())
star3.text = "%.1f".format((100.0/count.toDouble()) * listofStarsCounter.get(2).toDouble())
star4.text = "%.1f".format((100.0/count.toDouble()) * listofStarsCounter.get(3).toDouble())
star5.text = "%.1f".format((100.0/count.toDouble()) * listofStarsCounter.get(4).toDouble())
} catch (e : ArithmeticException) {
}
}
})
}
}
すぎsumAllvalueを数え、感謝 –