I am a beginner in android programming with Kotlin. I'm close on this, but missing something. Google is not showing me just what I need. Hoping someone here can.
I'm using android studio 4.1.1
, kotlin
and Firebase
I have several records in my test Firebase database.
each of these records has data, but when I use println
in my code one part is showing null and it is not hitting another println
at all.
my code - in part
WeightExercise.kt:
...
// i write records to the db like this:
val ref = FirebaseDatabase.getInstance().getReference("weights")
val recordID = ref.push().key
val userId = Global.userID
if (recordID != null){
ref.child(userId).child(recordID).setValue(weight).addOnCompleteListener{
Toast.makeText(applicationContext,"Weight Record rated successfully",Toast.LENGTH_LONG).show()
}
}
...
DisplayWeightsActivity.kt:
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val ref = FirebaseDatabase.getInstance().getReference("weights")
val uidRef = ref.child("weights").child(uid)
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
println("$$$$$$ dataSnapshot: ${dataSnapshot.toString()}")
for (ds in dataSnapshot.children) {
println("#$#$#$#$#$ in override dataSnapshot.children")
val muscle = ds.child("muscleGroup").getValue(String::class.java)
val exercise = ds.child("exerciseText").getValue(String::class.java)
val date = ds.child("date").getValue(String::class.java)
Log.d("TAG", date + " " + muscle + " " + exercise)
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("DB ERROR", databaseError.message) //Don't ignore errors!
}
uidRef.addListenerForSingleValueEvent(valueEventListener)
}
In my Logcat window I see this:
$$$$$$ dataSnapshot: DataSnapshot { key = oB3pzvhAr6foyhGZUuvFl4CECOw2, value = null }
But the println("#$#$#$#$#$ in override dataSnapshot.children")
does not print.
The key above oB3pzv...
matches my userid, so it sees that. I'm not sure what value = null
means. Is it not seeing the children records? How do I get the children records under weights >> userId ?
val uidRef = ref.child(uid)
– John Cowan Jan 06 '21 at 17:15