Home > Software engineering >  Update value from realtime database after button click
Update value from realtime database after button click

Time:01-15

I'm trying to create a like functionality in my app, the like function is already working (increment and decrement). My problem is I need to re-enter the activity again to see the new value of the like. But the value in my realtime database is already changed but in the app view, it doesn't increment or decrement (Need to re-enter to see the new value). How do i refresh a value in my app when in a button click? The code is below

//set the likes number on start
likes.text = myPlants.likes.toString();

//When like is clicked
bulbLike.setOnCheckListener(object : OnCheckListener {
    override fun onChecked(view: ExpressView?) {
        bulbLike(myPlants.plantId,currentUser!!.uid)

        //When i remove this the values dosent change
        val rootRef = FirebaseDatabase.getInstance().reference
        val likeRef = rootRef.child("plants").child(myPlants.plantId).child("likes")
        likeRef.get().addOnCompleteListener(OnCompleteListener<DataSnapshot?> { task ->
            if (task.isSuccessful) {
                val value: Long? = task.result.getValue(Long::class.java)
                likes.text = value.toString()
            } else {
                Log.d("TAG", "Error") //Don't ignore potential errors!
            }
        })
    }

    override fun onUnChecked(view: ExpressView?) {
        bulbDislike(myPlants.plantId,currentUser!!.uid)

        //When i remove this the values dosent change
        val rootRef = FirebaseDatabase.getInstance().reference
        val likeRef = rootRef.child("plants").child(myPlants.plantId).child("likes")
        likeRef.get().addOnCompleteListener(OnCompleteListener<DataSnapshot?> { task ->
            if (task.isSuccessful) {
                val value: Long? = task.result.getValue(Long::class.java)
                likes.text = value.toString()
            } else {
                Log.d("TAG", "Error") //Don't ignore potential errors!
            }
        })

    }
})

This one works it changes the value but it changes to 1 or -1

This is method or like and dislike

    private fun bulbLike(plantId: String, userId: String) {

        val dPlant: DatabaseReference = FirebaseDatabase.getInstance().reference
        dPlant.child("plants").child(plantId).child("likes").setValue(ServerValue.increment(1))
        dPlant.child("plants").child(plantId).child("userLikes").child(userId).child("status").setValue("Liked")
    }

    private fun bulbDislike(plantId: String, userId: String) {

        val dPlant: DatabaseReference = FirebaseDatabase.getInstance().reference
        dPlant.child("plants").child(plantId).child("likes").setValue(ServerValue.increment(-1))
        dPlant.child("plants").child(plantId).child("userLikes").child(userId).child("status").setValue("Dislike")

    }

CodePudding user response:

This here gets the data once, don't use it in your case:

//THIS WONT LISTEN TO UPDATES:

 likeRef.get().addOnCompleteListener(OnCompleteListener<DataSnapshot?> { task ->
if (task.isSuccessful) {
val value: Long? = task.result.getValue(Long::class.java)
likes.text = value.toString()
} else {
Log.d("TAG", "Error") //Don't ignore potential errors!
}
})

Read like this instead, using ValueEventListener, like this:

//THIS WILL LISTEN TO UPDATES DIRECTLY

val postListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {

//set likes here
val value = dataSnapshot.getValue(Long::class.java)
likes.text = value.toString()

}

override fun onCancelled(databaseError: DatabaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException())
}
}
likeRef.addValueEventListener(postListener)
  • Related