I have a list of child nodes inside a root node. I want to read a specific key-value pair which is present inside each child node using Kotlin for Android. How can I achieve that functionality.
For instance here is the list of all child nodes directly attached to the root node :
Here is the child node "azd1" which have STATUS = 1
Here is the second child node "bzi12" with STATUS = 0
I want to write a Kotlin code which returns the status value for each node, along with the reference of child name.
Currently I am doing something like this
val listener = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
Timber.d("Values: ${snapshot.value}")
}
override fun onCancelled(error: DatabaseError) {
Timber.d("Error: ${error.message}")
}
}
databaseRef
.addValueEventListener(listener)
}
but instead of returning status for each child I currently get all the child nodes with all their key-value
CodePudding user response:
Since you are listening to a list of nodes with a value listener, you will need to loop over the children of the resulting snapshot in onDataChange
to get to the individual results.
for (childSnapshot in snapshot.children) {
print(childSnapshot.child("STATUS").value);
}