得到孩子的孩子

时间:2017-01-01 15:18:27

标签: android firebase firebase-realtime-database

我正在尝试让一个孩子在彼此里面,并使用Firebase和MaterialDrawer Libarary

将相同排列添加到抽屉中的折叠视图中

以下是在onCreate()

中运行的方法的代码
private void addDrawerItems() {
    RootRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            result.addItem(IntentItemDrawer = new ExpandableDrawerItem()
                    .withName(String.valueOf(dataSnapshot.getKey()))
                    .withIcon(GoogleMaterial.Icon.gmd_collection_case_play)
                    .withIdentifier(t++).withSelectable(false).withSubItems(
                            new SecondaryDrawerItem()
                                    .withName(String.valueOf(RootRef.child(dataSnapshot.getKey()).child(dataSnapshot.getKey())))
                                    .withLevel(2)
                                    .withIcon(GoogleMaterial.Icon.gmd_8tracks)
                                    .withIdentifier(t++)));

错误在行.withName(String.valueOf(RootRef.child(dataSnapshot.getKey()).child(dataSnapshot.getKey())))中我要在其中显示子名称的子项

修改:

这是我的数据库结构

enter image description here

我想要做的是将他们的键/名称放在抽屉中的相同结构中

像这样,但不是出现的链接,我想显示

enter image description here

test1,test2,该死,ddddd ......等等

编辑2:

我试图分解该方法,但NullPointerException我认为我可以工作但需要一些修改

  1. addDrawerItems()

    {....
    .withIdentifier(t++).withSelectable(false).withSubItems(testMethod(dataSnapshot)));}
    
  2. TestMethod的(dataSnapshot)

    private List<IDrawerItem> testMethod(DataSnapshot dataSnapshot) {
        RootRef.child(dataSnapshot.getKey()).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                createSub(dataSnapshot);
            }
    ....
        });
        return null;
    }
    
  3. createSub(dataSnapshot)

    private void createSub(DataSnapshot dataSnapshot) {
        new SecondaryDrawerItem()
                .withName(String.valueOf(dataSnapshot.getKey()))
                .withLevel(2)
                .withIcon(GoogleMaterial.Icon.gmd_8tracks)
                .withIdentifier(t++);
    }
    

1 个答案:

答案 0 :(得分:1)

所以概念是这样的:

RootRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(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 this
        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {

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

        }
    }
}

我还没有足够的知识来回答你在MaterialDrawer Library上的实际实现,但我希望你能填补其余部分。

修改

我想我可能会在不知道图书馆的情况下尝试提供帮助。如我错了请纠正我。试试这个:

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

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

    result.addItem(parentItem);
}

注意:如果withSubItems只取一个值,则替换它。我认为它不会起作用。但是,自评论你提到它可以,我想你知道这样做的方法。

编辑2:

这是onChildChange()方法:

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

RootRef.addChildEventListener(new ChildEventListener() {
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        // code from my first edit here

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

    public void onChildChange(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();
    }