Retrieve data from Firebase

1

Is there any way to retrieve Firebase data from any ".child ()" that is found? So far I have only recovered data by delivering the address of the data in my Android code ...

This is what I do now, example:

DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference();
zonesRef.child("ZONE_1").child("a1").child("a11");

This is another example, here I want to find the data "user1" that is inside all those child (). How could I look for it without specifying each of those child (.child ("Companies" + Country) .child (City) .child (CompanyType) .child (userUid)), since these child () are changing depending on what select the user.

Here I want to find the data "user1" without having to describe the entire child () string to find the value

My question is if there is a way to retrieve the data from ".child (" a11 ")" without the need to do all that string, just giving the reference of the Database.

I hope you can help me. Of course, thank you very much for your time!

    
asked by Matías Nicolás Núñez Rivas 20.06.2018 в 21:38
source

1 answer

1

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

    
answered by 21.06.2018 / 02:40
source