To make it work you just have to make a POJO class to retrieve the user's data
This class will be called UserPojo
and will have the following
public class UserPojo {
private String Usuario;
public UserPojo(){
}
public UserPojo(String usuario) {
Usuario = usuario;
}
public String getUsuario() {
return Usuario;
}
public void setUsuario(String usuario) {
Usuario = usuario;
}
}
and then I get the user of each reference
mDatabase.child("Empresas Chile").child("Puerto Montt").child("Otro tipo de empresas").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserPojo usuario = dataSnapshot.getValue(UserPojo.class);
//Obtenemos los valores que queres
String usuario = usuario.getUsuario();
Log.e("Nombre de usuario: " , "" + usuario );
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
where mDatabase
is
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
and uid
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
String uid = mAuth.getCurrentUser().getUid();
If you want to iterate and get all the user names in your node "Other types of companies" just add a for
mDatabase.child("Empresas Chile").child("Puerto Montt").child("Otro tipo de empresas").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
UserPojo usuario = snapshot.getValue(UserPojo.class);
//Obtenemos los valores que queres
String usuario = usuario.getUsuario();
Log.e("Nombre de usuario: " , "" + usuario );
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
Remember that the above method is the same if you change the child, the for will go through all the elements under the first child that you assign looking for the values defined by the class pojo, getChildren()
will get all the children of the keys under the main node (child) that you choose
Note: I would recommend you not to use spaces in the names of your parent nodes, since you might have problems calling them inside the child