我试图获取Firebase数据库中各部分的名称并将其添加到arraylist中,但它只返回null。
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
答案 0 :(得分:0)
要解决此问题,请更改以下代码行:
mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/");
与
mSectionReference = database.getReference().child("apartments").child("B3").child("sections");
将子名称作为参数传递时,不需要斜杠。
答案 1 :(得分:0)