roge solis
roge solis

Reputation: 25

Retrieving Multiple Data Firebase

I'm new in firebase, so I'm trying to know how can get all the names of each of the products in the table Products, thank you

enter image description here

Upvotes: 0

Views: 742

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138814

To the name, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = rootRef.child("Products");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                Log.d("TAG", name);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
productsRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

ps4
Asus
galaxy 8

Upvotes: 1

Related Questions