[英]Getting parent node through child node in Firebase Realtime database
In my app in Android studio I've coded for allowing users to book appointment with doctors.在我在 Android 工作室的应用程序中,我编写了允许用户预约医生的代码。 So if the doctor accepts the appointment, I want to change the value of child node "Status".
所以如果医生接受预约,我想改变子节点“状态”的值。 For that I need the value of the parent node which is
为此,我需要父节点的值,即
.
.
For that I tried using String currentUserId = ds.getKey();
为此,我尝试使用
String currentUserId = ds.getKey();
,but ds,getKey() gives the value "Patient" which is a child node of the required parent node. ,但是 ds,getKey() 给出的值是“Patient”,它是所需父节点的子节点。 So how do I get the key (circled red in image)?
那么我如何获得钥匙(图中红色圆圈)?
patRef = database.getReference().child("Patient Appointments");
patRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
String pat = ds.child("Patient").getValue(String.class);
if(pat.equals(Email)){
HashMap update= new HashMap();
update.put("Status","Appointment on "+date);
String currentUserId = ds.getKey();
Toast.makeText(DoctorNotification.this, currentUserId, Toast.LENGTH_SHORT).show();
patRef.child(currentUserId).updateChildren(update).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
Toast.makeText(DoctorNotification.this, "Appointment accepted", Toast.LENGTH_SHORT).show();
}
});
flag1="True";
}
}
if(flag1.equals("False")){
Toast.makeText(DoctorNotification.this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
});
As you discovered yourself, you can update the current node in the loop with:正如您自己发现的那样,您可以使用以下命令更新循环中的当前节点:
ds.getRef().child(currentUserId).updateChildren(update)
I wanted to provide an additional comment on the approach you take to finding the node to update.我想就您查找要更新的节点所采取的方法提供额外的评论。 Right now you load all
Patient Appointments
to find one of them, which is going to be inefficient - especially one you start adding more appointment.现在,您加载所有
Patient Appointments
以找到其中一个,这将是低效的 - 尤其是您开始添加更多预约的那个。
One way to improve this is by using a query to only retrieve the appointment of the Patient
you're looking for.改善这一点的一种方法是使用查询来仅检索您正在寻找的
Patient
的约会。 That'd look something like this:看起来像这样:
patRef = database.getReference().child("Patient Appointments");
Query patientQuery = patRef.orderByChild("Patient").equalTo(Email);
patientQuery.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
...
And you can then remove the if(pat.equals(Email)){
from within the onComplete
.然后您可以从
onComplete
中删除if(pat.equals(Email)){
。
One smaller optimization after this is that you probably only need to check the appointments that are pending.在此之后的一个较小的优化是您可能只需要检查待处理的约会。 For that you'd like a query like above, but then on two fields.
为此,您想要一个像上面这样的查询,但是在两个字段上。
This is not something the Firebase Realtime Database natively supports, but you can emulate it by adding a composite field to your data: "Patient_Status": "Elise@doc.com_Pending"
.这不是 Firebase 实时数据库本身支持的东西,但您可以通过向数据添加复合字段来模拟它:
"Patient_Status": "Elise@doc.com_Pending"
。 The query would then become:然后查询将变为:
patRef.orderByChild("Patient_Status").equalTo(Email+"_Pending");
The final improvement assumes that the doctor is actually shown a list of all (pending) appointments and then click to accept one or more of them.最后的改进假设医生实际上看到了所有(待定)约会的列表,然后单击以接受其中的一个或多个。 In such a scenario you already read all the appointments from the database to show them in the user interface.
在这种情况下,您已经从数据库中读取了所有约会以在用户界面中显示它们。
If you store the key of each appointment in a (hidden) field in the user interface, you can then use that key when the doctor clicks it, and the entire update operation can become as simple as:如果您将每个约会的密钥存储在用户界面的(隐藏)字段中,那么您可以在医生单击时使用该密钥,整个更新操作可以变得如此简单:
patRef
.child("keyOfTheAppointmentTheDoctorClicked")
.child("Status")
.setValue("Appointment on "+date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.