[英]How to get the key value of a specific child in the Firebase Realtime database (Android Development)?
How to get the key value of a specific child in the Firebase Realtime database (Android Development)?如何在 Firebase 实时数据库(Android 开发)中获取特定子项的键值? I want to get the value of a specific key according to the selected subject.我想根据所选主题获取特定键的值。 Suppose if the user selects java then I want to get ' -MnLZAaa1pmVpRusEG8s ' (in the blue box).假设如果用户选择java然后我想得到' -MnLZAaa1pmVpRusEG8s '(在蓝色框中)。 I tried to get the subject name and subject unique keys in the list and then compare it with the string selected by the user, but don't know why I am getting a null value.我试图获取列表中的主题名称和主题唯一键,然后将其与用户选择的字符串进行比较,但不知道为什么我得到的是空值。 How can I do it?我该怎么做?
public String getSubjectUuid() {
List<String> subjectNamesList = new ArrayList<>();
List<String> subjectUuidList = new ArrayList<>();
DatabaseReference addedSubjectReference = FirebaseDatabase
.getInstance()
.getReference(classCreatorPhoneNo + "/" + "class_list" + "/" + classUuid + "/" + "added_subject");
addedSubjectReference
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
if (snapshot.exists()) {
subjectNamesList.add(snapshot.child("subject_name").getValue(String.class));
subjectUuidList.add(snapshot.child("subject_uuid").getValue(String.class));
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
for (int i = 0; i < subjectNamesList.size(); i++) {
if (selectedSubject.equals(subjectNamesList.get(i))) {
classKey = subjectUuidList.get(i);
break;
}
}
return classKey;
}
You're looking to use a query that orders/filters on your subject_name
field.您正在寻找使用对您的subject_name
字段进行subject_name
/过滤的查询。 Something like this should do the trick:像这样的事情应该可以解决问题:
addedSubjectReference
.orderByChild("subject_name") // 👈 First order on a property
.equalTo("java") // 👈 Then filter on a value
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//if (snapshot.exists()) { // 👈 This is not needed inside `onChildAdded`
subjectNamesList.add(snapshot.child("subject_name").getValue(String.class));
subjectUuidList.add(snapshot.child("subject_uuid").getValue(String.class));
//}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.