我正在做一个约会应用程序,此界面是向用户显示他们已经预约了哪位医生。
我已经做过这样的事情,现在我需要使用theraid来检索它们在Firebase中另一个表中的名称。有人可以教我怎么做吗?在此先感谢:)
这是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();
}
});
}
}
这是Recyclerview类。
@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();
}
}
答案 0 :(得分:1)
最直接的方法是为每个约会添加一个附加的侦听器,然后查找该约会的用户名:
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);
}
需要考虑的几件事: