D.Almeida
D.Almeida

Reputation: 75

Retrieve data of all user id from firebase

I'm try to populate listview with all messages of all users of my Android app, but, I got it populate listview with only one user messages because I'm inserting messages information in database this form: Usuario > UID > Mensagem > Key creating with push > Data of mensagem. For better verification follows image of the database on firebase. Structure of database firebase

Can help me, please? Hugs.

Upvotes: 1

Views: 3476

Answers (1)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126654

database.child("usuario").addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
         for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
             for (DataSnapshot messageSnapshot: snapshot.child("mensagem").getChildren()) {
                 listView.add(messageSnapshot.child("textoMensagem").getValue().toString());
             }
         }
    }

    ...

As you can see I add a ValueEventListener to retrieve all children nodes of "usuario" and then loop through its children to get to your messages.

Tip: Think about (at least) posting your code/database structure etc. in English, it could make reading your question a lot easier.

Upvotes: 3

Related Questions