简体   繁体   English

如何从子节点的值中获取父节点的名称?

[英]How to get the name of the parent node from the values of child nodes?

Here is the preview of the tree: 这是树的预览:

在此处输入图片说明

I tried to do this: 我试图这样做:

mRef.orderByChild("desc").equalTo(des).addChildEventListener(new ChildEventListener()
{
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s)
    {
        mRef.orderByChild("userid").equalTo(uid).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s)
            {
                myParentNode = dataSnapshot.getKey();
            }
    });
});

But it returns the value of the previously visited node. 但是它返回先前访问的节点的值。

Note: The Parent node "ASSIST Blog" contains many children which may have similar values. 注意:父节点“ ASSIST Blog”包含许多可能具有相似值的子级。

You should call the getKey() method in your retrieved dataSnapshot : 您应该在检索到的dataSnapshot调用getKey()方法:

final Query userQuery = mRef.orderByChild("First Name");

  userQuery.addChildEventListener(new ChildEventListener() {
     @Override
     public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        map.clear();
        //Get the node from the datasnapshot
        String myParentNode = dataSnapshot.getKey();
        for (DataSnapshot child: dataSnapshot.getChildren())
        {
           String key = child.getKey().toString();
           String value = child.getValue().toString();
           map.put(key,value);
        }

You cannot achieve this with Firebase Realtime database without some changes to your database. 如果不对数据库进行一些更改,则无法使用Firebase Realtime数据库实现此目的。 Firebase Realtime database does not have the capability to perform filtering on multiple conditions. Firebase Realtime数据库无法在多种条件下执行过滤。 In SQL terms, cannot use "multiple where clauses". 用SQL术语,不能使用“多个where子句”。 If you want to check for matches on multiple properties, you'll have to create a composite field that contains a combination of the things you're looking for. 如果要检查多个属性的匹配项,则必须创建一个复合字段,其中包含要查找的内容的组合。 For example, you'll need a string value that is composed from your desc property concatenated with your userid property and for this please see my answer from this post . 例如,您需要一个字符串值,该字符串值由desc属性和userid属性连接而成,为此,请参见我在这篇文章中的回答。

If you are interested, Firestore allows you to filter on multiple conditions and for that I recommend you see the official documentation . 如果您有兴趣,Firestore允许您在多种条件下进行过滤,为此,我建议您参阅官方文档

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

相关问题 如果有许多同名的父节点,如何从xml的父节点中选择所有子节点? - How to select all child nodes from a parent node from xml if there are many parents node with same name? 如何使用Java从XML读取父节点的子节点 - How to read child nodes of a parent node from XML using java 从父级获取子节点(xml,java) - get child nodes from parent (xml, java) 如何从树中的任何父节点获取所有叶节点 - How to get all leaf nodes from any parent node in a tree XMLUnit 比较 XML 不起作用,其中父节点包含多个具有相同名称和属性列表但属性值不同的子节点 - XMLUnit compare XML not working where parent node contains multiple child nodes of same name and attributes list but different attribute values 通过使用XPath搜索父节点来获取子节点 - Get child nodes by searching for parent node using XPath 如何删除父节点将其所有子节点? - How to delete a parent node will all its child nodes? 如何从父类获取执行的Java子类名称? - How to get executed Java Child class name from Parent class? 我们如何使用 Android 代码获取根或任何节点的子节点的名称? - How do we get name of child nodes of root or any node using Android code? 如果父节点和子节点具有相同的名称,如何查找节点? - How to find a node if parent node and child node has same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM