I want to fetch a variable called "progress" from my Realtime database, like this:
Now, I would like to assign this 1 as a variable, so I wrote code like this:
val progresstab = FirebaseDatabase.getInstance().getReference("prProgressTableRow")
However, I don't exactly know how to call that value. How can I make a variable so that it will assign it as the value 1?
I tried this:
val progression = parseInt(progresstab.child("progress").toString())
However, it did not work. How can I get that value directly?
I tried creating a variable:
progression = parseInt(progresstab.child("progress").toString())
However it doesn't show me anything and the app even crashes. I tried making listeners, but it fetches the value from a new class instead of me just getting the snapshot.
Assuming that the prProgressTableRow
node is a direct child of your root ref, then if you want to read the value of progress
field that exists at the following database reference:
db
|
--- prProgressTableRow
|
--- progress: 1
You have to create a reference that points to the progress
node and call get() as in the following lines of code:
val db = Firebase.database.reference
val progressRef = db.child("prProgressTableRow").child("progress")
progressRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val progressSnapshot = task.result
val progress = progressSnapshot.getValue(Int::class.java)
Log.d(TAG, "$progress")
} else {
Log.d(TAG, "${task.exception?.message}") //Never ignore potential errors!
}
}
If you want to use the value of progress
outside the callback, then I recommend you use one of the solutions that exists in the following post: