I just retrieve a list of data from a table named 'appointment'. From this table, I will need to get a data named 'theraid' in order to get the name from another table named 'thera'. I have no idea how to do it in android java. Is there someone can guide me? Thanks in advance.
Here is my table structure in firebase
Here is my code, but it does not work for me. Java class
databaseReference= FirebaseDatabase.getInstance().getReference().child("appointment");
databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
String tid = dataSnapshot.child("theraid").getValue(String.class);
a.add(thera);
refThera = FirebaseDatabase.getInstance().getReference("alluser").child("thera");
refThera.child(tid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot2: dataSnapshot.getChildren()) {
String text = dataSnapshot2.child("name").getValue(String.class);
tname.add(text);
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
throw databaseError.toException();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
}
});
adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a,tname);
rv.setAdapter(adapter);
RecyclerView class
@Override
public void onBindViewHolder(@NonNull final MyRecyclerviewPAppointment.MyViewHolder holder, int position) {
holder.tname.setText(name.get(position).charAt(position));
holder.tdate.setText(alist.get(position).getDate());
holder.ttime.setText(alist.get(position).getTiming());
}
Try the following:
databaseReference = FirebaseDatabase.getInstance().getReference().child("appointment");
databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
String theraid = dataSnapshot1.child("theraid").getValue(String.class);
DatabaseReference refThera = FirebaseDatabase.getInstance().getReference().child("alluser").child("thera");
refThera.child(theraid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
throw databaseError.toException();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
throw databaseError.toException();
}
});
First retreive the theraid
inside the appointment
node then add another databaseReference
and use the theraid
to be able to retrieve the name
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments