Search code examples
androidjsonfirebasefirebase-realtime-databasepojo

Get data from firebase realtime database in android


I am trying to get data from firebase realtime database in android. I have created a json data file and imported the json file in firebase database. The data file looks like this :

{
    "data": [
        {
        "name" : "one",
        "age" : "ten"
        },{
        "name" : "two",
        "age" : "twenty"
        }
    ]
}

I have created a POJO class. Now how can I get all the json objects from the array? I've searched for a while but can't understand what will be the reference & child here.


Solution

  • I would recommend changing your Firebase structure as following:

    • replace [] with {} as Firebase recommends using this practice
    • add a specific unique id for every user(or item) you have in data node.

    Take this example:

    enter image description here

    As you can see in the picture every user has a specific id. Firebase provides for every user an unique id which you can get with String userId= user.getUid();.

    You can create unique ids aswell and get them using String key = Database.child("users").push().getKey(); (not advised to be used for user ids)

    If you wish to read the data you structured I recommend reading this.

    Also read this.