How to access child node in Firebase?

Asked

Viewed 535 times

0

I have the following tree on Firebase:inserir a descrição da imagem aqui

How do I access to list the data within the node "holes"?

I am using the following data listing method:

public void recuperaFuros(){
    DatabaseReference furosRef = firebaseRef
            .child(idUsuario.toString())
            .child("vhykFsTJMhaCd6jkBd3oXpmYoiH2Thu Jan 10 22:54:24 GMT-02:00 2019")
             .child("furos")
            .child("Thu Jan 10 22:54:51 GMT-02:00 2019");

    furosRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            furos.clear();
            for(DataSnapshot ds: dataSnapshot.getChildren()){

                furos.add(ds.getValue(Furo.class));

                //System.out.println("PROJETO" + ds.getValue());
            }
            adapterFuros.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

but my return is being:

 Can't convert object of type java.lang.String to type geoapp.cursoandroid.com.geoapp.model.Furo

on the line

furos.add(ds.getValue(Furo.class));

My rules are like this:

{
  "rules": {
      ".read" : true,
      ".write" : true
    }
}

2 answers

0

What happens is, in the loop for, every iteration it takes a property of the object, then the value of ds.getValue is the value of the property alvoFuro, i.e., "alv"

Try removing the last call to the method child

//...
DatabaseReference furosRef = firebaseRef
        .child(idUsuario.toString())
        .child("vhykFsTJMhaCd6jkBd3oXpmYoiH2Thu Jan 10 22:54:24 GMT-02:00 2019")
         .child("furos");

furosRef.addValueEventListener(//...
  • The error disappeared, however, my Recycler does not bring the data back

  • If I delete the line that gives error and debug with System.out.println(ds.getValue) it brings my data back

  • Then this problem has been solved and you can accept the answer. Ask another question if you cannot solve the other problem!

0

Friend, first you should structure your BD better.

Instead of using an Id followed by the time of the request, try the following exit:

idUsuario (idProjeto) // You will agree with myRef.Child("Users"). Child(user.getUid()). push(). Child("Timestamp"). setValue(Servervalue.TIMESTAMP); //Other data

The push will make you have a unique ID. And the timestamp will be dropped, you will replace it with "dataHoraCadastro". The timestamp will be a data that you can easily convert when needed. Instead of dealing with this time and date you are using. Timestamp gives you full date, up to milliseconds.

//Inside holes, you will do the same thing. Instead of being the full date as id, you can use push() or timestamp.

myRef.Child("Users"). Child(user.getUid()). push(). Child("holes). Child("Timestamp"). setValue(Servervalue.TIMESTAMP); //Other Data And to avoid redundancy in the "dataHoraCadastro" part, you can use firebase, the.getKey() datasnapsho. (That instead of taking the data, you will have access to the key, which would be your Timestamp, or date, if you prefer to keep).

public void recuperaFuros(){
    DatabaseReference furosRef = firebaseRef
            .child(idUsuario.toString())
            .child("vhykFsTJMhaCd6jkBd3oXpmYoiH2Thu Jan 10 22:54:24 GMT-02:00 2019")
             .child("furos")
            .child("Thu Jan 10 22:54:51 GMT-02:00 2019");

    furosRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            furos.clear();
           if(dataSnapshot.exist())`{
                furos.add(dataSnapshot.getValue(Furo.class));
}
                //System.out.println("PROJETO" + ds.getValue());
            }
            adapterFuros.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

// You are using foreach to get unique data. If the class is with the same variables that are in the BD, they will be assigned normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.