1

Reading the documentation I understand how to pull a single value from the database but I'm not sure how to pull multiple from the same place. For example, I am looking to rank students in order of their grade. So I use orderByChild("grade") which is an int, and then I wish to get the first ten, so I use limitToLast(10) because the higher grades will be last. I am confused as to how I iterate through and push the data to the correct variables.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gradeRef = rootRef.child("test_scores").child("english_grades");
Query gradeRef = gradeRef.orderByChild("grade").limitToLast(10);
gradeRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        // Handling the post

        int gradeOne = 0;
        int gradeTwo = 0;
        int gradeThree = 0;
        String studentOne = "";
        String studentTwo = "";
        String studentThree = "";
        for (DataSnapshot ds: dataSnapshot.getChildren()) {
            gradeOne = ds.child("grade").getValue(Integer.class);
            gradeTwo = ds.child("grade").getValue(Integer.class);
            gradeThree = ds.child("grade").getValue(Integer.class);
            studentOne = ds.child("student").getValue(String.class);
            studentTwo = ds.child("student").getValue(String.class);
            studentThree = ds.child("student").getValue(String.class);
        }
        gradeOne.setText(Integer.toString(gradeOne));
        gradeTwo.setText(Integer.toString(gradeTwo));
        gradeThree.setText(Integer.toString(gradeThree));
        studentNameTV.setText(studentOne);
        studentNameTV2.setText(studentTwo);
        studentNameTV3.setText(studentThree);
    }

{
  "test_scores" : {
    "english_grades" : {
      "-Lo0W8ks7WCEsrym5Qpl" : {
        "name" : "Melissa",
        "grade" : 88
      },
      "-Lo0W92WqeUMdq_y7J8M" : {
        "name" : "Tom",
        "grade" : 95
      },
      "-Lo0W9KjzXE_XCU4K8MN" : {
        "name" : "Andrew",
        "grade" : 89
      }
    }
  }
}

What I want to see from my results would be StudentName = "Tom" Grade = "95" StudentName = "Andrew" Grade = "89" StudentName = "Melissa" Grade = "88".

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sean O
  • 238
  • 3
  • 14

1 Answers1

1

To set your data to different views, please use the following for loop:

for (DataSnapshot ds: dataSnapshot.getChildren()) {
    String name = ds.child("name").getValue(String.class);
    String grade = ds.child("grade").getValue(Integer.class);

    if(name.equals("Tom")) {
        gradeOne.setText(Integer.toString(grade));
        studentNameTV.setText(name);
    }

    if(name.equals("Andrew")) {
        gradeTwo.setText(Integer.toString(grade));
        studentNameTV2.setText(name);
    }

    if(name.equals("Melissa")) {
        gradeThree.setText(Integer.toString(grade));
        studentNameTV3.setText(name);
    }
}

Please note that the second property name is "name" and not "student".

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi Alex, thanks for your answer. It does correctly answer my question but if I had a much larger pool of students, and did not know which students scored the highest but still wished to order by "grade" and then pull the name and grade based on the highest grade, how would you do this? @AlexMamo – Sean O Sep 06 '19 at 14:05
  • Good to hear that I have correctly answered your question. For a larger number of students, the text views won't help you. In that case you should use a `ListView`, or even better a `RecyclerView`. If you intent to use a `RecyclerView`, you can see my answer from this **[post](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)**, where I have explained step by step how you can do this. – Alex Mamo Sep 06 '19 at 14:26