Reputation: 33
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 :)
Here is my firebase structure.
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
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:
Upvotes: 2