2

I have checked these and other questions that how to get data from dataSnapShot so please first read the question.

Below is the my Firebase DB structure where I am performing these all.

enter image description here

I have implemented below code to get/read data from Firebase. Issue is unable to get value by using that key or by passing that key directly. When I get key then its possible and printed also but its not working with value.

mDatabase!!.reference.child(FirebaseKey.NodeEmpCheckInOut) //-->EmpCheckInOut
            .child(AppConstant.yyyy_MM.format(Date()))     //-->2019-04
            .child(userSession!!.getUserId())              //-->N8OE2CGu8nav0k0n7Rh61tKjvRF3
            .addValueEventListener(object : ValueEventListener {
                override fun onCancelled(dataSnapshotError: DatabaseError) {
                    Log.e(TAG, "Error:Exception->" + dataSnapshotError.message)
                }

                override fun onDataChange(dataSnapShot: DataSnapshot) {
                    Log.i(TAG, "complete Data SnapShot::->$dataSnapShot")

                    for (dsnpNode: DataSnapshot in dataSnapShot.children) {
                        val key: String = dsnpNode.key.toString()
                        val value: Any? = dsnpNode.child(key).child("checkIn").getValue().toString()

                        //I have tried these ways also to retry data
                        //val value: String = dsnpNode.child(key).child("checkIn").value.toString()
                        //val value: String = dsnpNode.child("2019-04-11").child("checkIn").value.toString()

                        Logg.i(TAG, "key->$key")
                        Logg.i(TAG, "value::-> $value")

                        /*Log value is printed in log window
                          key->2019-04-11
                          value::-> null*/
                    }
                }

            })

When i print dataSnapShot.toString() then its printed as below in log window.

{ key = N8OE2CGu8nav0k0n7Rh61tKjvRF3, value = {2019-04-17={todayDate=2019-04-17, checkIn=05:41 AM, isCheckIn=2, checkOut=05:44 PM}, 2019-04-23={todayDate=2019-04-23, checkIn=03:29 PM, isCheckIn=1, checkOut=00}, 2019-04-12={todayDate=2019-04-12, checkIn=09:35 AM, isCheckIn=2, checkOut=10:00 PM}, 2019-04-16={todayDate=2019-04-16, checkIn=05:39 AM, isCheckIn=2, checkOut=05:39 PM}, 2019-04-18={todayDate=2019-04-18, checkIn=05:55 PM, isCheckIn=2, checkOut=08:13 PM}, 2019-04-13={todayDate=2019-04-13, checkIn=09:36 AM, isCheckIn=2, checkOut=5:00 PM}, 2019-04-19={todayDate=2019-04-19, checkIn=10:48 AM, isCheckIn=1, checkOut=00}, 2019-04-11={todayDate=2019-04-11, checkIn=05:35 AM, isCheckIn=2, checkOut=08:00 PM}, 2019-04-22={todayDate=2019-04-22, checkIn=12:10 PM, isCheckIn=1, checkOut=00}} }

where am I wrong?

James Z
  • 12,209
  • 10
  • 24
  • 44
Farmer
  • 4,093
  • 3
  • 23
  • 47

1 Answers1

3

To solve this, please change the following line of code:

val key: String = dsnpNode.key.toString()

to

val key: String = dsnpNode.key

Calling toString() doesn't help you at all since the keys are always strings.

And also change the following line of code:

val value: Any? = dsnpNode.child(key).child("checkIn").getValue().toString()

to

val value: Any? = dsnpNode.child("checkIn").getValue(String::class.java)

See, I have removed the call to .child(key) because there is no need for that.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193