John John - 27 days ago 20
Android Question

Retrieve all data under Uid Firebase Android

Hello guys I am new to Firebase and I am trying to retrieve all data under Uid.
I store them like this:

mDatabase=FirebaseDatabase.getInstance().getReference().child("Data").child(auth.getCurrentUser().getUid());
//some other code here
DatabaseReference newProduct =mDatabase.child(category).child("Product").push();
newProduct.child("productname").setValue(name);
newProduct.child("date").setValue(date);


My question is how can I retrieve all data under userUid category? Do I need something like
.child(*).child("Products")
?

Answer

What you need to do is use a listener to retrieve that data, something along the lines of

 mDatabase.child(category).child("Product").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                 String name = (String) dataSnapshot.child("productname").getValue;
                 String date = (String) dataSnapshot.child("date").getValue;


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

There you go, now you pulled all that data as Strings.

Also, I would suggest that instead of

auth.getCurrentUser().getUid()

use

String uid = (String) auth.getCurrentUser().getUid();

and just put uid in there instead, that way your code is much cleaner and everytime you need to refer to uid, you have a string for it.