【问题标题】:Retrieve attendance from firebase real time database从 Firebase 实时数据库中检索出勤率
【发布时间】:2026-02-06 05:55:01
【问题描述】:

下面是我的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"
        }
     }
  }
}

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    您可以像这样查询 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());
                        }
                    }
                });
    

    此代码 sn-p 将为您提供 presentsBSCS 1st_Ac++ 类中的编号。可以根据需要修改得到其他presentsabsents和classes

    希望这会有所帮助:)

    【讨论】:

    • 我想得到每个学生的礼物和缺席的数量。
    • 我在我的问题中添加了一张图片。请检查一下。这就是我想要的。