alexandr camp
alexandr camp

Reputation: 23

how to make changes in a multiple child nodes of my database firebase with android

I want to know how you do the tour of my database as you spend Users-> Id-> Personas that I get it

in this way

enter image description here

my object person

public class Persona implements Serializable{


private String nombres;
private String address;
private String parentesco;
private int dni;
private int telefono;
private String img;
private String latitud;
private String longitud;
private String dirCoordenadas;

public Persona(){
    super();
}

public Persona(String nombres, String address, String parentesco, int telefono,
               int dni, String img,String latitud,String longitud, String dirCoordenadas) {
    this.nombres = nombres;
    this.address = address;
    this.parentesco = parentesco;
    this.telefono = telefono;
    this.dni = dni;
    this.img=img;
    this.latitud=latitud;
    this.longitud=longitud;
    this.dirCoordenadas=dirCoordenadas;
}

this is my Firebase connection instance

databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Personas")

how do I get that child that represents the name of each person so that inside I keep the changes in each Persona

 Map<String,Object> update= new HashMap<>();
 update.put("latitud",lati);
 update.put("longitud",longit);
 update.put("dirCoordenadas",dir);
 databaseReference.child("Personas").child(---Here--).updateChildren(update);

some idea of how to get the data of a tree of nodes where each branch is identified by the name of each person and inside contains their data and you want to change 3 data of each child node

Upvotes: 2

Views: 144

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

To be able to update multiple nodes, you need to know the exact path of each node.

It seems you already have a reference to a specific user. In that case, you will need to read the personas, loop over them, and then update each in turn. Something like this:

Map<String,Object> update= new HashMap<>();
update.put("latitud",lati);
update.put("longitud",longit);
update.put("dirCoordenadas",dir);
databaseReference.child("Personas").addListenerForSingleValueEvent(new ValueEventListener() {
  public void onDataChange(DataSnapshot snapshot) {
    for (DataSnapshot child: snapshot.getChildren()) {
      child.getRef().updateChildren(update);
    }
  }
  ...
)

Upvotes: 1

cpt
cpt

Reputation: 77

If I understand your question correctly, you want to write whole objects to the database.

If so, you just have to create the custom object. In your case this would look something like that:

public class Persona implements Serializable {
  public String name;
  public String dir;
  ...
  ...
  ...

  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return name;
  }
}

of course you would need all fields and for each field a public setter and getter.

When you have that, you then can just write the whole object to the Database with something like that:

FirebaseDatabase.getInstance().getReference().child("someChild").child("someID").setValue(persona);

Just keep in mind, that your object needs to implement serializable.

Upvotes: 0

Related Questions