faraz_ahmed_kamran faraz_ahmed_kamran - 2 months ago 8
Android Question

Firebase Database add node to JSON tree for android

I have the following structure in my Firebase Database JSON Tree:
enter image description here

Now I have the

requests
node as the following in Android:

DatabaseReference requestsNode


How can I add another node
request2
with all the information-childs (date, destination, origin, ...) to the
requests
node? At the time of execution I do not know how many requests are already in the tree. So I do not know the increment
request<number>
number.

Answer

You can use push() method to add new child instead of deciding the index for your child. It is based on timestamp so it will always unique and sorted chronologically.

So for your case

ref = FirebaseDatabase.getInstance().getReference("requests");
ref.push().setValue(requestObject);

Hope this helps, comment if you have questions :)

Comments