[英]How to use a data from one table as a reference to get another data in another table using Firebase?
[英]How to get a specific data and use it as a key to get another data from another table in firebase
我需要從一個表中獲取數據並將其用作參考,以便在另一個表中獲取另一個數據。
這是我在java類中獲取數據的方式。在我的表中,我存儲了一個名為'theraid'的屬性,我需要對其進行檢索並將其用作引用,以便在另一個表中獲取另一個名為'name'的屬性。
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);
final String theraid = dataSnapshot1.child("theraid").getValue().toString();
refThera = FirebaseDatabase.getInstance().getReference().child("alluser").child("thera");
refThera.child(theraid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {
for (DataSnapshot ds: dataSnapshot3.getChildren())
{
***************error this line below********************************
String text = ds.child("name").getValue().toString();
namelist.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,namelist);
rv.setAdapter(adapter);
這是recyclerview類,以便顯示通過java類檢索的數據。
public class MyRecyclerviewPAppointment extends RecyclerView.Adapter<MyRecyclerviewPAppointment.MyViewHolder> {
private Context context;
ArrayList<AppointmentObject> alist;
ArrayList<String> namelist1;
public MyRecyclerviewPAppointment(Context c, ArrayList<AppointmentObject> t,ArrayList<String> namelist) {
context = c;
alist = t;
namelist1=namelist;
}
@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(namelist1.get(position));
// holder.tstatus.setText(alist.get(position).getTiming());
}
@Override
public int getItemCount() {
return alist.size();
}
}
我不知道我的代碼有什么問題,但是它無法正常工作,只是一直停下來。 這是錯誤。 我可以知道d = code中的錯誤在哪里嗎? 感謝您的幫助:)。
在第二個偵聽器中,您的databaseReference
在node theraid
。 因此,您不必循環就可以訪問name
的值。 因此,更改以下代碼:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {
for (DataSnapshot ds: dataSnapshot3.getChildren()) {
String text = ds.child("name").getValue().toString();
namelist.add(text);
}
}
變成這個:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {
String text = dataSnapshot3.child("name").getValue().toString();
namelist.add(text);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.