从Firebase实时数据库检索出勤

时间:2019-01-07 10:19:36

标签: java android firebase firebase-realtime-database

下面是我的firebase结构。我想检索一个班级(从spinner中选择)和一个​​主题(从微调器中选择)的出勤率。我不想从firebase检索数据。我想要的是每个学科中每个学生的礼物和缺席的数量。请检查我想要的图像。
I want this kind of result.

"attendance":{  
"01-01-19":{  
  "BSCS 1st_A":{  
     "c++":{  
        "std1":{  
           "attendance":"absent",
           "id":"std1"
        },
        "std12":{  
           "attendance":"present",
           "id":"std12"
        },
        "std2":{  
           "attendance":"present",
           "id":"std2"
        },
        "std3":{  
           "attendance":"present",
           "id":"std3"
        },
        "std4":{  
           "attendance":"absent",
           "id":"std4"
        }
     },
     "software engineering":{  
        "std1":{  
           "attendance":"present",
           "id":"std1"
        },
        "std12":{  
           "attendance":"present",
           "id":"std12"
        },
        "std2":{  
           "attendance":"present",
           "id":"std2"
        },
        "std3":{  
           "attendance":"absent",
           "id":"std3"
        },
        "std4":{  
           "attendance":"absent",
           "id":"std4"
        }
     }
  }
},
"05-01-19":{  
  "BSCS 1st_A":{  
     "calculus":{  
        "std1":{  
           "attendance":"present",
           "id":"std1"
        },
        "std12":{  
           "attendance":"present",
           "id":"std12"
        },
        "std2":{  
           "attendance":"present",
           "id":"std2"
        },
        "std3":{  
           "attendance":"absent",
           "id":"std3"
        },
        "std4":{  
           "attendance":"present",
           "id":"std4"
        }
     }
  }
}

1 个答案:

答案 0 :(得分:0)

您可以像这样查询firebase数据库:

final DatabaseReference dinosaursRef;
dinosaursRef.child("01-01-19").child("BSCS 1st_A").child("c++").whereEqualTo("attendance", "present")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        QuerySnapshot querySnapshot = task.getResult();
                        int totalPresent= querySnapshot.size();
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

此代码段将为您提供presentsBSCS 1st_A类中的c++个数字。您可以根据需要进行修改,以获取其他presentsabsentclasses

希望这会有所帮助:)