0

I have the following structure

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

I need to get the section number (Section1, section2...) and for every section, the lastname for student2.
The problem that i facing is that it is all in a Fragment class in android that I need to display and nothing seems to be working. I have referred this for getting the children too, but the for loop somehow does not work and I get Null values on retrieval.

I need to get

section1, lastname (of student2 in section1)
section2, lastname (of student2 in section2)
...
and so on until all sections are exhausted
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
evan
  • 431
  • 1
  • 6
  • 14

2 Answers2

0

U can make a model class like

class Class{
Arrylist<section> sections;
};

class section{
Arrylist<Student> students;
};

class Student{
String firstname, lastname;
};

then get the value of classes

have other way, without making many model classes, but that is more painful and time consuming . If u want that feel free to comment.

0

Please use the following code:

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);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193