2017-08-04 107 views
0

我有以下结构获取子值火力地堡

root 
    -class1 
    -section1 
     -student1 
      -firstname 
      -lastname 
     -student2 
      -firstname 
      -lastname 
    -section2 
     -student1 
      -firstname 
      -lastname 
     -student2 
      -firstname 
      -lastname 

我需要的部分数(SECTION1,第2节......)并为每节中,姓氏为STUDENT2。
我面对的问题是,它全部在android中的片段类中,我需要显示并且没有任何东西似乎正在工作。我也提到this也是为了让孩子们,但for循环以某种方式不起作用,我得到值检索。

我需要得到

section1, lastname (of student2 in section1) 
section2, lastname (of student2 in section2) 
... 
and so on until all sections are exhausted 

回答

0

U可以作出这样

class Class{ 
Arrylist<section> sections; 
}; 

class section{ 
Arrylist<Student> students; 
}; 

class Student{ 
String firstname, lastname; 
}; 

然后一个模型类得到的类

值有其他的方式,而不会使得许多模型类,但这更加痛苦和耗时。如果你想随时发表评论。

+0

我想看看这一点。谢谢 – evan

+0

获取Object类中的值。 HashMap data =(HashMap )dataSnapshot.getValue(); 然后将对象分解成您想要的值。 –

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);