Return list from a child in firebase

Asked

Viewed 984 times

1

Good morning ! Guys, I have a situation in which I want to return in a list all the data whose one of the children are equal to id, in the image below I have the nodes structures of my database in firebase and I want to return for example the number of all activities whose one of the nodes (idUsuario) be the same as the logged-in user (in case I used a form of encryption to store the user id and the id I can recover normally), to improve, in a relational database would look something like this: select * from activities Where idUsuario = "A" for example;

inserir a descrição da imagem aqui

Well, to solve this problem I changed the structure I was using to save the data in firebase so it would look like this: inserir a descrição da imagem aqui

This way I can bring more easily the data that is tied to the indicated user:

//Evento de consulta
    valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //LIMPANDO A LISTA
            atividadeArrayList.clear();

            //Listando cada uma das atividades criadas pelo usuário
            for (DataSnapshot dados: dataSnapshot.getChildren()){ //recupera os filhos do nó principal
                Atividade atividade = dados.getValue(Atividade.class);
                atividadeArrayList.add(atividade);
            }

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

But thank you Matheus for your help !

1 answer

3


So, the problem is that you add a "key" to the string "profile, if you always have access to this key that comes before profile, you can make a comparison in Query:

Query query = ref.orderByChild('idUsuario').equalTo('seuID');
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot querySnapshot : dataSnapshot.getChildren()) {
            MyClass id = dataSnapshot.getValue(MyClass.class); // ou String id = dataSnaptshot.getValue(MyClass.class).toString();
            if (id.MyID.equals('xxxxxxxxperfil')) {
                console.log(querySnapshot.getKey());
            }
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});
  • Matheus, I’m testing your code here, but I’m having a doubt, in case you requested your Movie class as Movie, but I got confused, you can explain to me the following code logic: String id = dataSnapshot.getValue(Movie.class); if (Movie.getLead().equals('xxxxxxxxprofile')) { console.log(querySnapshot.getKey()); }

  • I had taken an example that I used, and had not completely customized, I apologize. So the Myclass, are say the values you implemented in firebase understand? With getValue you can race them

  • This link would be a perfect reference for you to have a look at: https://stackoverflow.com/questions/42950532/android-extracting-data-from-firebase-data-snapshot concerteza irá ajudar muito em suas dúvidas

  • Well, I changed my mind and hit the bank to stay that way : +ACTIVITIES + idUsuario('xxxxxxxxxxperfil') + idAtivity + Description + title + time + id + idUsuario ...

  • Maravilha Eduardo

Browser other questions tagged

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