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