检索5节点深度系统中的第3个节点

时间:2017-11-25 19:11:17

标签: android firebase-realtime-database

我试图获取Firebase数据库中各部分的名称并将其添加到arraylist中,但它只返回null。

Firebase Node
这是我的代码:

   mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/");
    ValueEventListener sectionListner = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("sections", "onDataChange");

            if (dataSnapshot.exists()) {
                Log.d("sections", "snapshot exists" );

                for (DataSnapshot sectionSnapshot : dataSnapshot.getChildren()) {
                    if (sectionSnapshot != null) {
                        Section section = sectionSnapshot.getValue(Section.class);
                        Log.d("sections", "Section created: "  + section.getName());

                    } else
                        Log.d("sections", "sections null");
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            Log.w("failedSnap", "Failed to read value.", error.toException());
        }
    };
    mSectionReference.addValueEventListener(sectionListner);

这是logcat的结果:

onDataChange
snapshot exists
Section created: null
Section created: null

2 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/");

mSectionReference = database.getReference().child("apartments").child("B3").child("sections");

将子名称作为参数传递时,不需要斜杠。

答案 1 :(得分:0)

解决方案:

我发现尽管路径中有一个元素,但它并不意味着有一个"对象"那里。

我实现了一个新的hashmap,它将该部分的名称保存为键和值,使结构如下所示: firebase database strcture