在5节点深度系统中检索第3个节点
问题描述:
我试图获取Firebase数据库中各节的名称并将其添加到arraylist,但它只返回null。在5节点深度系统中检索第3个节点
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
答
为了解决这个问题,请更改这行代码:
mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/");
与
mSectionReference = database.getReference().child("apartments").child("B3").child("sections");
当你通过孩子的名字作为参数不需要的斜线。
include部分对象实现。 –