0

I used a query that take a specific date from the user to get data from the database but it keep saying that the data is noted existing even if I used two-loop one over the Uid and one over push key. here is the code I write.

final DatePickerDialog datePickerDialog = new DatePickerDialog(AdminAppointmentManage.this, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        month += 1;
        selectedDate = dayOfMonth + "/" + month + "/" + year;
        dateText.setText(selectedDate);
        Query query = FirebaseDatabase.getInstance().getReference().child("Appointment").orderByChild("dateOfAppointment").equalTo(selectedDate);
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                appointmentList.clear();
                List<String> appointmentKey = new ArrayList<>();
                if (dataSnapshot.exists()) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        for (DataSnapshot appointmentSnapshot : snapshot.getChildren()){
                            appointmentKey.add(appointmentSnapshot.getKey());
                            Appointments appointments = appointmentSnapshot.getValue(Appointments.class);
                            appointmentList.add(appointments);
                    }}
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(AdminAppointmentManage.this, "No Appointment for that day !!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}, year, month, day);

the database tree :

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ramiz Moh
  • 5
  • 2

1 Answers1

0

Firebase queries work on a flat list of child nodes. They can order/filter on a single property under each child node.

So you child("Appointment").orderByChild("dateOfAppointment").equalTo(selectedDate) takes each user node (the direct child nodes of Appointment and orders/filters then on dateOfAppointment. And since users don't have a dateOfAppointment property, that results in an empty list.

In your current data structure you can search across the appointments of a single user, but you can't search across the appointments of all users at once. If you want the latter, consider storing a single flat list of appointments (and then having the user ID in there as a property).

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thank you, Mr.Frank, but how can I solve this problem. if I edited the structure of the database could that help ?! – Ramiz Moh Oct 13 '19 at 16:17
  • My linked answers give a few different options of how to solve that. All of these will require you to change your data structure, as on your current structure it is not possible to search for appointments across all users. I added some more info to my answer too. – Frank van Puffelen Oct 13 '19 at 16:24