0

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.

screenshot of Firebase db

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 ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
John Cowan
  • 1,452
  • 5
  • 25
  • 39
  • Your code is reading from `/weights/weights` (double), which doesn't exist in your JSON. My guess is that one of these lines is not supposed to have the `weights` node in there: `val ref = FirebaseDatabase.getInstance().getReference("weights")` or `val uidRef = ref.child("weights").child(uid)`. – Frank van Puffelen Jan 06 '21 at 02:40
  • Thank you @FrankvanPuffelen That was it. so blind am i. I changed the second one to: val uidRef = ref.child(uid) – John Cowan Jan 06 '21 at 17:15
  • Good to hear that John I voted to close your question as a typo, as I doubt anyone caught in the same situation will bump into it in the future. – Frank van Puffelen Jan 06 '21 at 17:23
  • Thanks, @FrankvanPuffelen I have a follow up question. Should I create a new thread? – John Cowan Jan 06 '21 at 19:23
  • A new thread for what? – Frank van Puffelen Jan 06 '21 at 19:53
  • I have all my data in the dataSnapshot.Each record looks like: DataSnapshot { key = oB3pzvhAr6foyhGZUuvFl4CECOw2, value = {-MQEY5E7228LT34LNpZ-={.... I pass it to my recyclerviewadapter as a dataSnapshot. In: fun onBindViewHolder(holder: WeightViewHolder, position: Int) { I do not know how to apply 'position' to get each record. Is this possible? I'm looking at converting the dataSnapshot to an ArrayList, but I've not done that before. so much to learn – John Cowan Jan 06 '21 at 20:56
  • 1
    Ah, I see. So you have another problem after fixing the one I commented about. In that case please open a new question with its own updated [MCVE](http://stackoverflow.com/help/mcve). It might be very similar to this one, but at the very least the double `weights/weight` should be fixed in there. – Frank van Puffelen Jan 06 '21 at 21:21
  • This may help you, https://stackoverflow.com/questions/32886546/how-to-get-all-child-list-from-firebase-android – rahat Jan 10 '21 at 18:21

0 Answers0