Return list from a child in firebase

1

Good morning! I have a situation in which I want to return a list of all the data whose children are equal to the id, in the image below I have the structures of the nodes of my bank in firebase and I want to return, for example, the number of all the activities which one of the nodes (idUsuario) is the same as the logged in user (in case I used an encryption form to store the id of the user 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;

Well,tosolvethisproblemIchangedthestructureIwasusingtosavethedatainfirebasesothatitwouldlooklikethis:

This way I can more easily bring the data that is tied to the specified 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!

    
asked by anonymous 25.09.2017 / 15:35

1 answer

3

So, the problem is that you add a "key" with the "profile" string, if you always have access to that key that comes before the 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) {

    }
});
    
25.09.2017 / 15:45