2

How can we access all UID users name and the UID users child value in Firebase database?

I have uploaded the pic of my database

I also use RecyclerView you tell me about Firebase query I want to retrieve data with FirestoreRecyclerOptions query :

I want to access name of users and the amount value as shown in fig like this

name:600

enter image description here

Jere
  • 1,196
  • 1
  • 9
  • 31

1 Answers1

1

To get all user names, of all User objects that exist under "Users" node, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
    productsRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String name = ds.child("name").getValue(String.class);
                Log.d(TAG, name);
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

Because there are several objects within the "Users" node, first you need to get a reference to that node, attach a listener and then loop through the results. The result in the logcat will be:

jm
...

If you want to use the Firebase-UI library, then simply pass the "usersRef" to the FirestoreRecyclerOptions's setQuery() method, as explained in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193