Skip to content Skip to sidebar Skip to footer

Get Child Of Child

I am trying to get a child of a child inside each other and add the to a collapsing view in the drawer with the same arrangement, using Firebase and MaterialDrawer Libarary Here is

Solution 1:

So the concept is like this:

RootRef.addChildEventListener(newChildEventListener() {
    @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
        // since this event called from root,// then dataSnapshot key will contain "Intent", "else", "sdsd"// and dataSnapshot value is their child node// knowing that, you should create your parent Drawer Item here,// and next, you want to add sub item to that parent Drawer item,// with your child data from that parent,// like "test1" and "test2" for "Intent" parent// you should achieve it like thisfor (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {

            // in here, childSnapshot.getKey() should return "test1",// then "test2", depending on the loop count.// and childSnapshot.getValue() should return "2"

        }
    }
}

I don't have enough knowledge yet to answer you with practical implementation on MaterialDrawer Library, but I hope you can fill the rest.

EDIT:

I think I might try to help without knowing that library. Correct me if I'm wrong. Try this:

publicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
    ExpandableDrawerItem parentItem = newExpandableDrawerItem()
                .withName(String.valueOf(dataSnapshot.getKey()))
                .withIcon(GoogleMaterial.Icon.gmd_collection_case_play)
                .withIdentifier(t++).withSelectable(false);

    for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
        parentItem.withSubItems(newSecondaryDrawerItem()
            .withName(String.valueOf(childSnapshot.getKey()))
            .withLevel(2)
            .withIcon(GoogleMaterial.Icon.gmd_8tracks)
            .withIdentifier(t++));
    }

    result.addItem(parentItem);
}

Note: if withSubItems only take one value then replace it. I think it won't work. But since in comment you mention that it can, I think you know the way to do that.

EDIT 2:

This is inside onChildChange() method:

final HashMap<String, ExpandableDrawerItem> parentItemMap = newHashMap<>();

RootRef.addChildEventListener(newChildEventListener() {
    publicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {

        // code from my first edit here

        parentItemMap.put(dataSnapshot.getKey(), parentItem);
    }

    publicvoidonChildChange(DataSnapshot dataSnapshot, String s) {

        ExpandableDrawerItem parentItem = parentItemMap.get(dataSnapshot.getKey());

        parentItem.withName( ... ) .... ; // like inside onChildCreate()// also do the dataSnapshot.getChildren loop here// but DONT add the result.addItem();
    }

Post a Comment for "Get Child Of Child"