Firebase - Child inside another child

0

I would like to know how to put a child inside another child in the firebase, for example, create a child that calls "Company" and inside that child put other childs with each seat inside, do not know if I was clear, help me by favor. PS: I'm using android studio. Thank you in advance.

    
asked by anonymous 24.09.2017 / 19:25

2 answers

1

You can use ref.child() in a non-existent child and give ref.push() to create. So just do it:

DatabaseReference empresaRef = ref.child("Empresa");

To access the venues:

DatabaseReference sedeRef = empresaRef.child("Sede");

And to create a new one is just:

Empresa empresa = new Empresa();
Sede sede = new Sede();
DatabaseReference empresaRef = 
ref.child("Empresa").push().setValue(empresa);
DatabaseReference sedeRef = empresaRef.child("Sede").push().setValue(sede);

However it is recommended to save using a unique ID instead of the name. To generate the ID use:

Empresa empresa = new Empresa();
Sede sede = new Sede();

String empresaID = ref.push().getKey();
DatabaseReference empresaRef = ref.child(empresaID);
empresaRef.setValue(empresa);

String sedeID = empresaRef.push().getKey();
DatabaseReference sedeRef = empresaRef.child(sedeID);
sedeRef.setValue(sede);

;)

    
26.09.2017 / 00:39
0

First you have to set the values you need, starting with your root, after root you go through the parent node and the parent you create your desired child.

Example:

Firebase newRef = ref.child("root").child("empresa").child("sede").push();
newRef.setValue("DollOnWorld");
    
25.09.2017 / 16:07