简体   繁体   English

如何在 android 工作室中从 firebase 检索多个子节点

[英]How to retrieve mulitple child nodes from firebase in android studio

I am trying to get data relating to a specific user in the database returned to a users screen in android studio.我正在尝试将与数据库中特定用户相关的数据返回到 android 工作室中的用户屏幕。 The data in firebase takes a format like so: firebase 中的数据格式如下:

"studentNumber" : {
        "1234567" : {
            "CS320" :{
                "Lab 1" : "80%",
                "Lab 2" : "90%"
            },
            "CS255" :{
                "Lab 1" :"30%"
            }
        },
        "6234567" : {
            "CS320" :{
                "Lab 1" : "70%",
                "Lab 2" : "100%"
            },

and so on like that.诸如此类。

I am trying to get all data/child nodes ie all modules, subsequent labs and subsequent grades relating to one singular student returned to their screen in android studio, however, so far I have only been able to get the data returning to the console in android studio instead of the application screen.我正在尝试将所有数据/子节点,即与一个单一学生相关的所有模块、后续实验室和后续成绩返回到 android 工作室的屏幕,但是,到目前为止,我只能将数据返回到控制台android studio代替应用画面。

This is the code I'm using to get the data.这是我用来获取数据的代码。

  databaseReference = FirebaseDatabase.getInstance().getReference("User").child("0000");
  databaseReference.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Log.v("E_Value","Data :" +dataSnapshot.getValue());
           }
   }

Any help would be great thanks.任何帮助都会非常感谢。

I didn't test the code, however the outline should look like this.我没有测试代码,但是大纲应该是这样的。

You should be fetching one key and call a function to obtain the value and the subsequent nodes related to it and their values.您应该获取一个键并调用 function 以获取值以及与其相关的后续节点及其值。

 databaseReference = FirebaseDatabase.getInstance().getReference("User").child("0000");
  databaseReference.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
             String studentKey = dataSnapshot1.getKey();
             fetchDetails(studentKey);
        }
     }
   }

   public void fetchDetails(String studentKey) {
       DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User").child("0000").child(studentKey);
       ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
             String courseKey = dataSnapshot1.getKey(); //this should print CS320
             fetchDetailsOfCourse(courseKey);
        }
        }
     }
   }

   public void fetchDetailsOfCourse(String courseKey) {

      System.out.System.out.println("Course key: " + courseKey);
       DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User").child("0000").child(studentKey).child(courseKey);
       ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String lab1 = dataSnapshot.child("lab1").getValue(String.class);
            String lab2 = dataSnapshot.child("lab2").getValue(String.class);

            System.out.println("Lab1 value: " + lab1); //this should get the values for lab1
            System.out.println("Lab2 value: " + lab2); //this should get the values for lab2

        }
        }
     }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM