thevikasnayak
thevikasnayak

Reputation: 629

Get firebase snapshot data in Kotlin Android

I'm new to Android & I don't know how to get all the firebase snapshot data at once inside the UID in the single data class.

  val usersPrivateRef = Constants.FIREBASE_RESIDENT_PRIVATE
    usersPrivateRef?.child("Fs0qczU3GsfJuGDGAeEN7bIgfjD3")
        ?.addListenerForSingleValueEvent(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                    if (snapshot.exists()) {
                        println(snapshot)
                    } else {
                        showLongToast("Snapshot not exists")
                    }
            }
            override fun onCancelled(error: DatabaseError) {}
        })

Upvotes: 1

Views: 1537

Answers (2)

thevikasnayak
thevikasnayak

Reputation: 629

I have found a solution for my above question With the help of Gson library & also I have created the Kotlin data classes same as my snapshot data.

private var residentDATA : ArrayList<MineUserInfo> = ArrayList<MineUserInfo>()

var gson = Gson()
val json = Gson().toJson(snapshot.value)
var data = gson?.fromJson(json, MineUserInfo::class.java)
residentDATA.add(data)

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

To get a value from the DataSnapshot you can use its child() and getValue calls. For example, to print Krishna's email, you'd do:

println(snapshot.child("personalDetails/email").getValue(String::class.java)

Upvotes: 2

Related Questions