1

How can I retrieve data with child* from Firebase Database and populate an User object class.

Firebase example:

  • users
    • uid : 131232
      • firstName : John
      • lastName : Doe
      • location
        • lat* : 15.2512312
        • lon* : -12.1512321
      • chats
        • -k1231* : true
        • -k1285* : true

and after having retrieved the data being able to use ie.: User.firstName or User.location.lat etc.

Thank you in advance.

Dimitri Leite
  • 1,791
  • 13
  • 16
  • To understand better, you want to query the database for all objects where `k1231` is eqaul to `true`? – Alex Mamo Nov 05 '18 at 08:30
  • My friend @Alex-Mamo, this is a Firebase Database model. I just want to query this User 1332 and cast it to the User.class object inside the app to work with the object. My main doubt is to know how to create the class User and the adapter to absorb all the items including the child "Chat" with its children. – Dimitri Leite Nov 05 '18 at 15:36

2 Answers2

2

As Sam Stern mentioned in his answer, it's best to create a representation for each class separately. I'll write you the corresponding classes in Kotlin.

This is the User class:

class User (
    val firstName: String = "",
    val lastName: String = "",
    val userLocation: UserLocation? = null
)

And this is the UserLocation class:

class UserLocation (
        val lat: Int = 0,
        val lng: Int = 0
)

to query this User 1332 and cast it to the User.class object

Please use the following lines of code:

val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().reference
val uidRef = rootRef.child("users").child(uid)
val valueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        val user = dataSnapshot.getValue(User::class.java)
        Log.d(TAG, "Lat/Lng: " + user!!.userLocation!!.lat + ", " + user.userLocation!!.lng);
    }

    override fun onCancelled(databaseError: DatabaseError) {
        Log.d(TAG, databaseError.message) //Don't ignore errors!
    }
}
uidRef.addListenerForSingleValueEvent(valueEventListener)

In which the uid should hold a value like 131232. The output in your logcat will be:

Lat/Lng: 15.2512312, -12.1512321

In the same way you can get: user!!.firstName and user!!.lastName.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi Dimitri! Have you tried my solution above, does it work? – Alex Mamo Nov 06 '18 at 07:43
  • 1
    Hey!!! Thank you very much @Alex-Mamo! It worked! Look what I got from my Log.d: "2018-11-07 01:14:07.971 22395-22395/com.app.firebaseusertest D/MainActivity: Lat/Lng: 15.2512312, -12.1512321". Your answer was really complete! – Dimitri Leite Nov 07 '18 at 03:17
  • My friend @alex-mamo what about the list of chats in the node chat? In firebase denormalized db this is very common: to have the references of other nodes under other node like: id = true. – Dimitri Leite Nov 09 '18 at 10:30
  • This sounds as a new challenge which basically should be considered another question that cannot be answered in a comment or using only the data above. Iin order to follow the rules of this comunity, please post another fresh question using a [MCVE](https://stackoverflow.com/help/mcve), so me and other users can help you. – Alex Mamo Nov 09 '18 at 10:33
  • 1
    Ok. Thank you my friend! – Dimitri Leite Nov 09 '18 at 10:36
  • Is that retrieving data from the database though? or is it coming from the class? – Adan Vivero Dec 12 '21 at 23:34
  • @AdanVivero Yes, it's from the database, – Alex Mamo Dec 13 '21 at 09:56
1

Your best bet is to create multiple custom classes:

class User {
  public String firstName;
  public String lastName;
  public UserLocation location;
}

...

class UserLocation {
   public double lat;
   public double lon;
}

Then you can deserialize the whole thing to User.

Sam Stern
  • 24,624
  • 13
  • 93
  • 124