0

I need to get the date and timing data by using the userid (DcEUNNJSB20WhmyL1IsJsk7YnQ1). Can someone guide me on this problem? Thanks in advance.

table

   databaseReference= FirebaseDatabase.getInstance().getReference("appointment");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String userid1 = FirebaseAuth.getInstance().getCurrentUser().getUid();


 for(DataSnapshot dataSnapshot1: dataSnapshot.child("appointment").getChildren()){
if(dataSnapshot1.getValue(AppointmentObject.class).getUserid().equals(userid1)){

    AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
    a.add(thera);

}
                   }

                adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a);
                rv.setAdapter(adapter);
            }
Kim E. R
  • 73
  • 1
  • 8

1 Answers1

0

You're looking for a query. In this case, since you're trying to order/filter on a child property, you'll want to use orderByChild(...). Something like:

databaseReference = FirebaseDatabase.getInstance().getReference("appointment").child("qNTrPz0B5hVlafqzRueEbbiUqj1");
Query query = databaseReference.orderByChild("userid").equalTo("DcEUNNJSB20WhmyL1IsJsk7YnQ1");

query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot childSnapshot: dataSnapshot.getChildren()){
            System.out.println(childSnapshot.getKey());
            System.out.println(childSnapshot.child("date").getValue(String.class));
        }
    }

    public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your reply. If I cannot get "qNTrPz0B5hVlafqzRueEbbiUqj1" as an id to put inside .child(), (since it is a therapist id and one user may book many appointments with many therapists) is there another way to do it? Or is there another way to get all the data by just matching the userid? That means ignoring all the parent key, straight away matching the data and fetch it. – Kim E. R Jul 31 '19 at 06:43
  • If you don't know the therapist, you can't query for the user. In that case your current structure makes it easy to find the users for a given therapist, but not to find the therapists for a given user. You'll need to add additional data to allow that use-case efficiently. See my answer [here](https://stackoverflow.com/q/40656589), or the one I just gave 2 hours ago [here](https://stackoverflow.com/a/57283430). – Frank van Puffelen Jul 31 '19 at 07:16