简体   繁体   中英

Retrieve all data from Firebase Database

ANDROID: I have a Firebase database like this. phoot

In my app, I would like to compile an arraylist that displays all values from the nameofEntry [DESCRIBED BELOW]. By this I mean the "balso," the "nairboh" and so on.

I have the references:

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference users = root.child("Users");
DatabaseReference childRef = users.child(userID);
DatabaseReference childRefNameNode = childRef.child(nameOfEntry);
childRefNameNode.child(nameOfEntry).setValue(nameOfEntry);

//FETCH DATA

childRefNameNode.child(nameOfEntry).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String valueFromDB = dataSnapshot.getValue(String.class);
                Log.i("Jimit", valueFromDB);


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

But this only fetches for one entry. How can I get more entries? [All of them]?

You need to use this code snippet. You haven't used any for loop to step over your children.

EDIT: Also, update your database reference as:

//Updated ref
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);*

    //add listener to updated ref
    ref.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                         //use the for loop here to step over each child and retrieve data                   
                         for (DataSnapshot childSnapshot : dataSnapshot.getChildren()){
                            String valueFromDB = childSnapshot.getValue(String.class);
                            Log.i("Jimit", valueFromDB);
                        }


                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

Do let me know if it changes anything for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM