简体   繁体   English

如何在 Firebase 实时数据库(Android 开发)中获取特定子项的键值?

[英]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.

相关问题 如何在 Firebase 实时数据库中的特定子树中推送值 - How to push value in specific child tree in Firebase Realtime Database 如何从 android 中的 firebase 实时数据库中获取随机密钥? - How to get random key from firebase realtime database in android? Firebase Realtime Database随机选择子节点后如何获取子节点的key - How to get the key of a child node in Firebase Realtime Database after choosing a child randomly 如何从 Firebase 获取特定键的特定子值 - How to Get specific Child Value From Firebase For Specific Key Android 和 Firebase 将 EditText 与实时数据库子值进行比较 - Android & Firebase comparing EditText to realtime database child value 如何获得由 Firebase 实时数据库为 Android 分配的随机密钥? - How do I get a random key assigned by Firebase Realtime Database for Android? 如何在firebase实时数据库android中的唯一键下获取特定节点 - how to get specific nodes under unique keys in firebase realtime database android 如何检查 firebase 实时数据库中是否存在特定值? - How to check for a specific value exist in the firebase realtime database? 在不知道 Firebase 实时数据库中的键值的情况下检索子值 - Retrieving child value without knowing key value from Firebase realtime database 从孩子内部的实时 Firebase 获取价值 - Get Value From Realtime Firebase Inside Child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM