to bring that data does the following
// Agregamos un listener a la referencia
ref.child("Usuario").child(userID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String nombre = dataSnapshot.child("nombre").getValue(String.class);
String numero = dataSnapshot.child("Numero").getValue(String.class);
String FechaNacimiento = dataSnapshot.child("FechaNacimiento").getValue(String.class);
String sexo = dataSnapshot.child("sexo").getValue(String.class);
}
//ya tenemos los datos desde Firebase, podemos actualizar la UI
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Fallo la lectura: " + databaseError.getCode());
}
});
where userID is the alphanumeric value where all the values of your node are stored.
String userID = mAuth.getCurrentUser().getUid();
Where mAuth is
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
and where ref is the reference to the database that is obtained in the following way
DatabaseReference ref;
ref = FirebaseDatabase.getInstance().getReference();
Important
Remember that the values you bring with Firebase are brought asyncronamente, so you will not be able to access that data outside the dataSnapshot
, because if you have not brought them yet, you will be null. And always remember to use dataSnapshot.exists()
to make sure you have values in that node and do not throw another NPE trying to go to a null reference.
Another tip is not always use the String data type to store all the data in your database, keep in mind that each type of variable has a certain space reserved in memory, it is not the same String as int, although do not notice in performance, it is better to save numbers that do not exceed 2 bytes (16 bits) in integers and not use String since we are allocating more memory when initializing that variable, but as I said, it is not an important impact to have in account, it's more of a design.
Another tip is to save the date as timestamp and not as dd/mm/yy
, if you save it as timestamp you will be able to sort users by date of birth, and you can obtain the timestamp from the client and transform it to date with a SimpleDateFormat