0

I am going to find student attendance by matching the registration number stored in realtime database. I am trying hard to get the student's registration no and the date when student visited school.

Here is my database picture and I want to get the date and registration number both and than display them in my view

final FirebaseDatabase databs= FirebaseDatabase.getInstance();
DatabaseReference p = databs.getReference("StudentAttend");
Query q= p.child("reg_no").orderByChild("date").equalTo(Parent_HomePage.childR)

Here is my query to fetch them. But the issue is that registration number and date have no names in database, they are keys. Kindly help me to fetch these attributes from database. Secondly kindly share Data snapshot code because my view doesn't show anything.

Thanks

2 Answers2

1

To get a key from realtime database you use getKey(). But i have to say you have to structure your data better. Also your query won't work as dates come before reg no. And ordering by date as shown will lead to issues for example 03-03-2021 will come before 20-02-2021.

       final FirebaseDatabase databs= FirebaseDatabase.getInstance();
       DatabaseReference p = databs.getReference("StudentAttend");

       p.addListenerForSingleValueEvent(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dns) {
               for (DataSnapshot snapshot : dns.getChildren()) {
                   String key = dns.getKey(); //this will get the date
                   //to get the student number within a date use above key
                   p.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                         @Override
                         public void onDataChange(DataSnapshot dns) {
                             for (DataSnapshot snapshot : dns.getChildren()) {
                                 String key = dns.getKey(); //this will get the reg_no
                                 //get pick and drop here

                    }
              }}}
                        
         }

To query data when you have a date

           final FirebaseDatabase databs= FirebaseDatabase.getInstance();
           DatabaseReference p = databs.getReference("StudentAttend").child(date_you_have); //then add listener here

If you don't have a date but have a reg no, follow above code to get the date key, then run query in there with

p.child(key).child(regnoyouhvae);

But i suggest you go over documentation again and read some articles on structuring your data, queries and more.

SABANTO
  • 1,316
  • 9
  • 24
  • Dates come before reg_no because every student attendance is saved relative to the dates. On which date, which students were present. – Rahat Batool May 17 '21 at 10:43
1

According to your last comment:

I want to show dates in my ListView whose child is 165.

To display the keys (dates) if the DataSnapshot object contains a child called "165", please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference studentAttendRef = rootRef.child("StudentAttend");
studentAttendRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                if (ds.child("165").exists()) {
                    Log.d("TAG", ds.getKey());
                }
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

14-03-2021
15-04-2021

To be able to display these dates in a ListView, please see my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193