Wanabe Super Coder
Wanabe Super Coder

Reputation: 183

How to access nested child values in Firebase

I have the following database structure. Click here for image of data structure.

I am trying to get database/content/xxxxxxx/matches for all the 'xxxxxxx' items under content into an Array list so that I can iterate the Array list and grab all the images for the items that match. For example, for Avery-Fit Solid Pant, I want to get the image for Blouse with Flared Sleeve, High-Neck Stripe Top, etc. and then move to Bell Sleeve Dress and do the same. Below is what I have tried, but it is not working.

 matchRef = FirebaseDatabase.getInstance().getReference().child("/content");

        matchRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                    SliderItems mvalue = dataSnapshot.getValue(SliderItems.class);
                    DataSnapshot contentSnapshot = dataSnapshot.child("/matches");
                    Iterable<DataSnapshot> matchSnapShot = contentSnapshot.getChildren();
                       for (DataSnapshot match : matchSnapShot){
                           SliderItems c = match.getValue(SliderItems.class);
                        matchImages.add(c);

                    }

                startMatchRecyclerView();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

I was pretty sure that my database was not structured properly and restructured it as follows. See alternate structure here. I think this is a more appropriate structure. I can access matches to populate an Array list. How do I iterate the Array list for each entry to find the members that are 'true' and then get the images from content for each 'true' member?

Upvotes: 1

Views: 2258

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

When you listen for the value of /content you get a snapshot that has all content in it. To get to the individual content items (which have a matches property), you need to loop over the children of the snapshot:

matchRef = FirebaseDatabase.getInstance().getReference().child("/content");

matchRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
            SliderItems mvalue = itemSnapshot.getValue(SliderItems.class);
            DataSnapshot contentSnapshot = itemSnapshot.child("/matches");
            Iterable<DataSnapshot> matchSnapShot = contentSnapshot.getChildren();
               for (DataSnapshot match : matchSnapShot){
                   SliderItems c = match.getValue(SliderItems.class);
                matchImages.add(c);

            }
        }
        startMatchRecyclerView();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

Alternatively you could use a ChildEventListener, in which case you can get rid of the for loop I added, since its onChildAdded will then already fire on what I called itemSnapshot in my code.

Upvotes: 1

Related Questions