Get data in Firebase Android

1

I would like to know how I can obtain data from a firebase database, I can now add the data, but I do not know how to consult that data.

This is my node in firebase:

If I wanted to bring the field "name" of that user, what would I have to do? I have used dataSnapshot and all that, but it brings me all the names that are in the database, I just want to bring a specific name.

In summary ... I add these data by means of a registry, I have enabled the firebase email authentication function, after registering when I login everything perfect, but I only want to bring the name of the current user, that is, if that id is logged, then just bring the name "Ibrahim".

I hope you have understood what I want to do.

    
asked by Ibrahim 12.12.2018 в 17:37
source

1 answer

1

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

    
answered by 12.12.2018 / 21:18
source