h.s
h.s

Reputation: 33

How to get data from another table according to a retrieved data in Firebase

I am doing an appointment application, this interface is to show to the user that which doctor they have booked an appointment.

I have already done something like this, now I need to use the theraid that I get to retrieve their name which is in another table in Firebase. Can someone teach me how to do it? Thanks in advance :)

myapplication

Here is my firebase structure. firebase

Here is the code in Java.

  a=new ArrayList<AppointmentObject>();
  namelist=new ArrayList<String>();

        databaseReference= FirebaseDatabase.getInstance().getReference().child("appointment");
        databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {


                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {

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


                }
               adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a,namelist);
                rv.setAdapter(adapter);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
            }
        });


    }
}

Here is the Recyclerview class.

@Override
    public void onBindViewHolder(@NonNull final MyRecyclerviewPAppointment.MyViewHolder holder,final int position) {

     holder.tdate.setText(alist.get(position).getDate());
      holder.ttime.setText(alist.get(position).getTiming());
      holder.tname.setText(alist.get(position).getTheraid());

    }

    @Override
    public int getItemCount() {
        return alist.size();
    }
}

Upvotes: 0

Views: 1668

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598951

The most direct way is to add an additional listener for each appointment, that then look up the user name for that appointment:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
databaseReference= rootRef.child("appointment");
databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
        for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
            String theraid = dataSnapshot1.child("theraid").getValue(String.class);
            DatabaseReference userRef = rootRef.child("alluser/thera").child(theraid);
            userRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot userSnapshot) {
                    String name = userSnapshot.child("name").getValue(String.class);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            }
        }
       adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a,namelist);
        rv.setAdapter(adapter);

    }

A few things to consider:

  • This loads the user name for each appointment, even if there are multiple appointments with the same user. In a more realistic scenario you'll want to keep a list of users you've already loaded, either for each appointment query or for a longer time.
  • While this load will be pretty fast since Firebase pipelines the requests over its existing connecting, it's still additional complexity on reading data. An alternatively would be to store the user name for each appointment in additional to their UID. This type of data duplication is quite common in NoSQL databases.

Upvotes: 2

Related Questions