我有以下结构
root
-class1
-section1
-student1
-firstname
-lastname
-student2
-firstname
-lastname
-section2
-student1
-firstname
-lastname
-student2
-firstname
-lastname
我需要获取部分编号(第1部分,第2部分......),并为每个部分提供student2的姓氏。
我面临的问题是它在android中的Fragment类中都需要显示,似乎没有任何工作。我已经提到this来获取孩子,但是for循环以某种方式不起作用,我在检索时得到 Null 值。
我需要
section1, lastname (of student2 in section1)
section2, lastname (of student2 in section2)
...
and so on until all sections are exhausted
答案 0 :(得分:0)
你可以制作像
这样的模型类class Class{
Arrylist<section> sections;
};
class section{
Arrylist<Student> students;
};
class Student{
String firstname, lastname;
};
然后获取类的值
有其他方式,没有制作许多模型类,但这更痛苦,更耗时。如果你想让它随意发表评论。
答案 1 :(得分:0)
请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classRef = rootRef.child("class1");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String lastname = ds.child("student2").child("lastname").getValue(String.class);
Log.d("TAG", lastname);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
classRef.addListenerForSingleValueEvent(eventListener);