我只是从名为“约会”的表中检索数据列表。从该表中,我将需要获取一个名为“ theraid”的数据,以便从另一个名为“ thera”的表中获取该名称。我不知道如何在android java中做到这一点。有没有人可以指导我?预先感谢。
这是我的代码,但对我不起作用。 Java类
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类
@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());
}
答案 0 :(得分:1)
尝试以下操作:
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();
}
});
首先在theraid
节点内检索appointment
,然后添加另一个databaseReference
并使用theraid
来检索name
。