get the name of all the users of my firebase

0

You want to get the name of all the users but I can not get to the node, I've been investigating, please any suggestions.

This is the code I use to give me an idea please.

I want to get it to later save it in a list and be able to manipulate it.

 database = FirebaseDatabase.getInstance();
    addU = database.getReference(bdreference.userRefence);
    addU.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            key= dataSnapshot.getKey();
            nombre=dataSnapshot.child(key).child("nombre").getValue(String.class);
                Log.i("nombre",nombre);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    
asked by devlp 13.12.2017 в 05:20
source

1 answer

0

You can get a list with the names iterating over dataSnapshot.getChildren() .

FirebaseDatabase.getInstance()
            .getReference()
            .child("usuarios")
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    List<String> usuarios = new ArrayList<>();

                    for (DataSnapshot childSnapshot : dataSnapshot.getChildren())
                        usuarios.add(childSnapshot.child("nombre").getValue(String.class));

                    // usuarios ahora contiene los nombres de todos los usuarios.
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
    
answered by 16.12.2017 в 08:41