用户预约后,将属性(user id, date, time)存储到以治疗师push id为父节点的预约表中(方便检索某位医生的预约)。
现在,在用户界面中,我需要使用用户 ID 获取所有属性。有谁知道该怎么做?
这是我的数据库结构
这是我自己的代码
a=new ArrayList<AppointmentObject>();
databaseReference= FirebaseDatabase.getInstance().getReference();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//to get current log in user id
String userid1 = FirebaseAuth.getInstance().getCurrentUser().getUid();
for(DataSnapshot dataSnapshot1: dataSnapshot.child("appointment").getChildren()) {
//if the userid stored is same as user id, it will be added to arraylist
if(dataSnapshot1.getValue(AppointmentObject.class).getUserid().equals(userid1)){
AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
a.add(thera);
}
}
adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a);
rv.setAdapter(adapter);
}
但是,出现这个错误
最佳答案
你的 NullPointerException 来自这一行
if(dataSnapshot1.getValue(AppointmentObject.class).getUserid().equals(userid1)) {}
其中一个字段为空 dataSnapshot1, dataSnapshot1.getValue(), dataSnapshot1.getValue().getUserid()
为确保您的代码将编译反向比较顺序:
if(userid1.equals(dataSnapshot1.getValue(AppointmentObject.class).getUserid())) {}
关于java - 如何使用其中一个属性值获取 Firebase 中同一个表中的另一个属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57215634/