简体   繁体   English

获取与另一个孩子具有相同父母的特定孩子 Firebase

[英]Get specific child with same parent as another child Firebase

I am making an Android app in Android Studio and I want to get specific data from the Firebase.我正在 Android Studio 中制作 Android 应用程序,我想从 Firebase 获取特定数据。 I am trying to search through the Firebase Realtime Database to get all of the Users that have a usercode of 1234 and then getting their name.我正在尝试搜索Firebase Realtime Database以获取所有用户usercode1234的用户,然后获取他们的姓名。 This is how I have it set up in Firebase:这就是我在 Firebase 中设置它的方式:

Firebase Setup image link Firebase 设置图片链接

Since I am searching for the data where the usercode = 1234 it will return (in the datasnapshot) all of the information about Joe and Lily (since they were the two users that had a usercode of 1234), but it shouldn't return information about Kevin.由于我正在搜索用户usercode = 1234的数据,它将返回(在数据快照中)关于 Joe 和 Lily 的所有信息(因为他们是用户代码为 1234 的两个用户),但它不应该返回信息关于凯文。 Here is how I did that:我是这样做的:

Query getUsers = ref.orderByChild("usercode").equalTo(1234);
    getUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                //User with usercode = 1234 exists
            } else {
                //User with that Usercode doesn't exist
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

The datasnapshot , if converted to a string would have all of Lily and Joe's information I believe.如果将datasnapshot转换为字符串,我相信它将包含 Lily 和 Joe 的所有信息。 What I want to do is get the first user's name (Which would be Joe in this case) from that datasnaphot .我想要做的是从该datasnaphot获取第一个用户的name (在这种情况下是 Joe)。 My code will then use it through a few tests before going to the second user's name (Which would be Lily) and repeating the tests.然后,我的代码将通过一些测试使用它,然后转到第二个用户的name (即 Lily)并重复测试。 Also, if there were 100 Users then I would need to repeat it for every one of those 100 that had a user code of 1234 , so I probably need something repeatable.此外,如果有 100 个用户,那么我需要为这 100 个用户代码为1234的每个用户重复它,所以我可能需要一些可重复的东西。

So somehow I need to find the first value of the child's name from all of the Users that have a usercode of 1234 and then repeat until I get all of the names and run the test on all of them.所以不知何故,我需要从所有用户usercode1234的用户中找到孩子name的第一个值,然后重复,直到我得到所有的名字并对所有的名字运行测试。

I am new to Stackoverflow, so please tell me if I am missing any information that would be helpful in answering the question.我是 Stackoverflow 的新手,所以请告诉我是否遗漏了任何有助于回答问题的信息。

---- EDIT ---- - - 编辑 - -

I have now figured out how to do this with Firebase Firestore.我现在已经弄清楚如何使用 Firebase Firestore 来做到这一点。 It has simpler queries and allows you to do more with the results.它具有更简单的查询,并允许您对结果进行更多操作。 If anyone else has the same issue that I stated above, Firebase Firestore is something you might want to try out.如果其他人遇到我上面提到的相同问题,您可能想尝试一下 Firebase Firestore。

When using the following line of code:使用以下代码行时:

Query getUsers = ref.orderByChild("usercode").equalTo("1234");

You are searching through the Users node, all users that have the usercode property set to "1234" .您正在搜索Users节点,所有将usercode属性设置为"1234"的用户。 This query will always return no results because you are searching for 1234 as a String and not as a number, as it is stored in the database.此查询将始终不返回任何结果,因为您正在搜索1234作为字符串而不是数字,因为它存储在数据库中。 The correct query should be:正确的查询应该是:

Query getUsers = ref.orderByChild("usercode").equalTo(1234); //No double quotes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM