Search code examples
androidfirebase-realtime-databaseandroid-recyclerviewfirebaseuikotlin-android-extensions

How to get specific values from all UserID (UID) and save in RecyclerViewFirebaseUI


How can I get the UID from all users from my database with the structure below and save it in my FirebaseRecyclerviewUI? I have tried for several hours but my recycler view still displays null.

-User
 |
  -uid
   |
    -advert
     |
      -bus_name
      -category
      -purl
      -id

enter image description here

Below is my getter/setter BusinessListData class that serves as the model for receiving the values and sending them to recyclerview

class BusinessListData {

    var id: String = ""

    var propixurl: String = ""

    var business_name: String = ""
    var category: String = ""

    constructor(){

    }

    constructor(id: String, propixurl: String, business_name: String, category: String) {
        this.id = id

        this.propixurl = propixurl
        this.business_name = business_name
        this.category = category
    }

    fun getID(): String{
        return id
    }
    fun getUrl(): String{
        return propixurl
    }
    fun getBusName(): String{
        return business_name
    }

    fun getCate(): String{
        return category
    }

    fun setID(id: String){
        this.id = id
    }
    fun setUrl(propixurl: String){
        this.propixurl = propixurl
    }
    fun setBusName(business_name: String){
        this.business_name = business_name
    }
    fun setCate(category: String){
        this.category = category
    }
}

My FirebaseRecyclerViewUI details are below. The mDatabase is the reference to the database and I tried getting the snapshot of the data which was successful according to my Log.v values. The problem is that I could not get the specific values under the Advert tree in my Firebase RealTime Database

mDatabase = FirebaseDatabase.getInstance().reference

val options: FirebaseRecyclerOptions<BusinessListData> = FirebaseRecyclerOptions.Builder<BusinessListData>()
    .setQuery(mDatabase, object: SnapshotParser<BusinessListData> {

        override fun parseSnapshot(snapshot: DataSnapshot): BusinessListData {
                Log.v("snapshotfire", snapshot.toString()) //returns all snapshot values
                Log.v("snapshotfire", snapshot.child("purl").toString()) // this returns null
                Log.v("snapshotfire", snapshot.child("bus_name").getValue().toString()) //this also returns null

            return BusinessListData(snapshot.child("id").getValue().toString(),
                snapshot.child("purl").getValue().toString(),
                snapshot.child("bus_name").getValue().toString(),
                snapshot.child("category").getValue().toString()
            )
        }

    })
    .build()

Solution

  • When you set a certain query or database reference to the FirebaseUI adapters, it will show each child node of that reference/query. Since you pass in the root of your database, your parseSnapshot will be called for each child node of the root, which does not seem to be what you want.

    To show the child nodes of the User node, pass in a reference to that node:

    mDatabase = FirebaseDatabase.getInstance().reference
    
    val options: FirebaseRecyclerOptions<BusinessListData> = FirebaseRecyclerOptions.Builder<BusinessListData>()
        .setQuery(mDatabase.child("User"), object: SnapshotParser<BusinessListData> {
            ...
    

    Now your parseSnapshot will be called for each child node of User, and you can get the necessary keys/values from that snapshot.

    For example, to get the UID you can either read the snapshot's key or its advert/uid property:

    override fun parseSnapshot(snapshot: DataSnapshot): BusinessListData {
        Log.v("snapshotfire", snapshot.key)
        Log.v("snapshotfire", snapshot.child("advert/id").getValue())
    )