0

I have a Firebase Database with several purchases in Android app, points value is assigned for each purchase, then entries are showing by following way:

FireBase Database

When userId is log in, a button appears and when user click on it the "showInfoClient" function is called and it must show user and the amount of points. Code is:

private val database = Firebase.database
private val myref = database.getReference("compras")

fun showInfoClient(view: View) {

        val userlog = FirebaseAuth.getInstance().currentUser?.displayName
        val alertDialogInfo = AlertDialog.Builder(this).create()
        myref.orderByChild("userId").equalTo(userlog).addListenerForSingleValueEvent(object :
            ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                var sum = 0
                for (data in dataSnapshot.children) {
                    sum += data.child("puntos").getValue(Int::class.java)!!
                    Toast.makeText(this@Principal, sum, Toast.LENGTH_SHORT).show()
                }
            }

            override fun onCancelled(databaseError: DatabaseError) {}
        })

        alertDialogInfo.setTitle(userlog)
        alertDialogInfo.setMessage("Puntos: ") // + sum

        alertDialogInfo.setButton(
            AlertDialog.BUTTON_POSITIVE, "OK"
        ) { dialog, _ ->; dialog.dismiss() }

        alertDialogInfo.show()

        val btnPositive = alertDialogInfo.getButton(AlertDialog.BUTTON_POSITIVE)

        val layoutParams = btnPositive.layoutParams as LinearLayout.LayoutParams
        layoutParams.weight = 100f
        btnPositive.layoutParams = layoutParams
    }

I have tried to use different options but i´m not able to set "sum" value on

alertDialogInfo.setMessage("Puntos: $sum")

Thanks in advance

1 Answers1

1

Any code that needs data from the database needs to be inside onDataChange or be called from there. Code outside of onDataChange will (most likely) run before the data is loaded.

So:

val userlog = FirebaseAuth.getInstance().currentUser?.displayName
val alertDialogInfo = AlertDialog.Builder(this).create()
myref.orderByChild("userId").equalTo(userlog).addListenerForSingleValueEvent(object :
    ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        var sum = 0
        for (data in dataSnapshot.children) {
            sum += data.child("puntos").getValue(Int::class.java)!!
        }
        alertDialogInfo.setTitle(userlog)
        alertDialogInfo.setMessage("Puntos: ") // + sum

        alertDialogInfo.setButton(
            AlertDialog.BUTTON_POSITIVE, "OK"
        ) { dialog, _ ->; dialog.dismiss() }

        alertDialogInfo.show()

        val btnPositive = alertDialogInfo.getButton(AlertDialog.BUTTON_POSITIVE)

        val layoutParams = btnPositive.layoutParams as LinearLayout.LayoutParams
        layoutParams.weight = 100f
        btnPositive.layoutParams = layoutParams
    }

    override fun onCancelled(databaseError: DatabaseError) {
        throw databaseError.toException(); // never ignore errors
    }
})

For a longer explanation see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for all, problem was userlog not same that userId, displayName vs email, then i correct it `val userlog = FirebaseAuth.getInstance().currentUser?.email` – user3137353 Dec 04 '20 at 20:20