2017-12-02 10 views
0

私のコードのカウント星(5つ星)と私はそれがリアルタイムで更新された場合、それは正しくないKotlinでFirebase OnDataChangeの中に地図firebase で

(今Firebase)

  1. を=カウント、問題を抱えています1
  2. = 2
  3. = 0
  4. = 0
  5. = 0

および変更firebase後

  1. = 3
  2. = 4
  3. = 0
  4. = 0
  5. = 0
[1 + = 1を追加します]

しかし、私はアプリを再起動した場合、それは正常な結果に変更します。

  1. = 2
  2. = 2
  3. = 0
  4. = 0
  5. = 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) { 

      } 
     } 
    }) 
} 

}

答えて

0

、あなたのArrayListは当初、このようになります:あなたは1つ星の評価、あなたが得るスナップショットに1を追加すると

[ 1 , 1 , 0 , 0, 0 ] 

:リスナーに実際になり全体の更新リストを含み、
[ 2 , 1 , 0 , 0, 0 ] 

あなたは本質的ではコースの結果のどの、あなたの元に、この更新されたリストを追加しているので、予想外の結果が取得している理由:あなたが開始するため

[ 3 , 2 , 0 , 0 , 0 ] 

は、このアプリの修正を再起動もう一度オールゼロリストを使用して、リスナーで受信したデータをそのデータに追加します。


したがって、問題を解決するには、既存の値に追加する代わりに更新を受け取ったときに、リストのすべての値を上書きするだけです。これを行うには、ループを実行する前にリストをすべてゼロにリセットします。

+0

すぎsumAllvalueを数え、感謝 –

関連する問題