如何从android中的firebase数据库检索特定的节点 [英] How to retrieve specific node from firebase database in android

查看:165
本文介绍了如何从android中的firebase数据库检索特定的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的android应用程序中使用Firebase。我正在阅读保存检索
但教程中使用的示例数据库(Dragon)与我的数据库结构不同。



这是将数据推送到firebase的代码

  Firebase myFirebaseRef = new Firebase ( https://myfirebaseurl.firebaseio.com/android/saving-data/fireblog); 
User userName = new User(socialNum,name,datofBirth,mob1,mob2,healthCondition);
Firebase usersRef = myFirebaseRef.child(name);
地图< String,User> users = new HashMap< String,User>();
users.put(name,userName);
myFirebaseRef.push()。setValue(users);

创建这样的数据库格式

<$ p
android:{
saving-data:{
fireblog:{
-JiRtkpIFLVFNgmNBpMj:{
$ bName:{
birthDate:100,
fullName:Name,
healthCond:fyhft,
mob1 :5855,
mob2:5858,
socialNumber:100
}
},
-JiRv0RmHwWVHSOiZXiN:{
mast:{
birthDate:100,
fullName:mast,
healthCond:fyhft,
mob1:5855,
mob2:5858,
socialNumber:100
}
}
}
}
}
}

我想要从Firebase中检索数据,如果我将全名放在我的应用程序搜索框中,它应该检索该特定节点,以便我可以在ListView中填充该信息。
$ b

这是我如何检索,

  final String Find = find.getText( )的ToString(); //获取搜索编辑文本框的文本
Firebase myFirebaseRef = new Firebase(https://myfirebaseurl.firebaseio.com/android/saving-data/fireblog);
查询queryRef = myFirebaseRef.orderByChild(fullName);
// System.out.println(dataSnapshot.getKey()+is+ value.get(socialNumber));
System.out.println(Find);

queryRef.addChildEventListener(new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String previousChild){
System.out.println(dataSnapshot。 getValue());
Map< String,Object> value =(Map< String,Object>)dataSnapshot.getValue();

String name1 = String.valueOf(value.get fullName));
//System.out.println(dataSnapshot.getKey()+is+ value.get(fullName)。toString());
if(name1。等于(Find)){
System.out.println(Name+ value.get(fullName));
}
else {
System.out.println (its is null);
}

}

但它返回所有的节点,

  02-19 12:18:02.053 8269-8269 / com.example.nilesh.firebasetest I / System.out:name 
02-19 12:18 :05.426 8269-8269 / com.example.nilesh.firebasetest I / System.out:{Name = {socialNumber = 100,birthDate = 100,fullName = Name,mob1 = 5855,mob2 = 5858,healthCond = fyhft}}
02-19 12:18:05.426 8269-8269 / com.example.nilesh.firebasetest我/ System.out:它是空的
02-19 12:18:05.426 8269-8269 / com.example。 nilesh.firebasetest I / System.out:{mast = {socialNumber = 100,birthDate = 100,fullName = mast,mob1 = 5855,mob2 = 5858,healthCond = fyhft}}
02-19 12:18:05.426 8269-8269 / com.example.nilesh.firebasetest I / System.out:它是空的

我可以检索特定的节点,以便如果我输入fullName = mast,它应该只检索与该节点中的所有字段的第二个节点。

解决方案

 查询queryRef = myFirebaseRef.orderByChild(fullNam E); 

像这样,查询通过它们的 fullName 值。但是它还没有限制返回哪个子节点。



要限制节点,还必须过滤。例如:

  Query queryRef = myFirebaseRef.orderByChild(fullName)。equalTo(gooner); 

您也可以通过使用 startAt 和/或 endAt 而不是 equalTo



正如加藤所说:


看起来好像有一个多余的孩子。数据结构为 saving-data / fireblog /< record id> / fluff / ... actual data .. 。并且绒毛层需要被删除为那些查询工作。您无法查询子女的子女。



I am trying to use Firebase in my android application. I am following documentation for Saving and retrieving, But the Sample database(Dragon) which is used in tutorial has different structure than my database.

This is my code for Pushing the data to firebase

Firebase myFirebaseRef = new Firebase("https://myfirebaseurl.firebaseio.com/android/saving-data/fireblog");
                User userName = new User(socialNum, name, datofBirth, mob1, mob2, healthCondition);
                Firebase usersRef = myFirebaseRef.child(name);
                Map<String, User> users = new HashMap<String, User>();
                users.put(name, userName);                    
              myFirebaseRef.push().setValue(users);

which create database format like this

{
      "android" : {
        "saving-data" : {
          "fireblog" : {
            "-JiRtkpIFLVFNgmNBpMj" : {
              "Name" : {
                "birthDate" : "100",
                "fullName" : "Name",
                "healthCond" : "fyhft",
                "mob1" : "5855",
                "mob2" : "5858",
                "socialNumber" : "100"
              }
            },
            "-JiRv0RmHwWVHSOiZXiN" : {
              "mast" : {
                "birthDate" : "100",
                "fullName" : "mast",
                "healthCond" : "fyhft",
                "mob1" : "5855",
                "mob2" : "5858",
                "socialNumber" : "100"
              }
            }
          }
        }
      }
    }

I want to Retrieve data from firebase such that, if I put "full Name" in my apps search box, it should retrieve that specific node, so that I can populate that information in Listview.

This is How I am trying to retrieve,

final String Find = find.getText().toString();   //Get text for search edit text box
                Firebase myFirebaseRef = new Firebase("https://myfirebaseurl.firebaseio.com/android/saving-data/fireblog");
                Query queryRef = myFirebaseRef.orderByChild("fullName");
               // System.out.println(dataSnapshot.getKey() + "is" + value.get("socialNumber"));
                System.out.println(Find);

                queryRef.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String previousChild) {
                        System.out.println(dataSnapshot.getValue());
                        Map<String,Object> value = (Map<String, Object>) dataSnapshot.getValue();

                        String name1 = String.valueOf(value.get("fullName"));
                    //System.out.println(dataSnapshot.getKey() + "is" + value.get("fullName").toString());
                    if (name1.equals(Find)){
                        System.out.println("Name" + value.get("fullName"));
                    }
                    else{
                        System.out.println("its is null");
                    }

                    }

but It returns all the nodes,

02-19 12:18:02.053    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ name
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ {Name={socialNumber=100, birthDate=100, fullName=Name, mob1=5855, mob2=5858, healthCond=fyhft}}
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ its is null
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ {mast={socialNumber=100, birthDate=100, fullName=mast, mob1=5855, mob2=5858, healthCond=fyhft}}
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ its is null

How can i Retrieve specific node so that If I enter fullName = mast, it should retrieve only second node with all the fields in that node.

解决方案

You're creating a query in this line:

Query queryRef = myFirebaseRef.orderByChild("fullName");

Like that the query orders the child nodes by their fullName value. But it doesn't yet limit what child nodes are returned.

To limit the nodes, you also have to filter. e.g.:

Query queryRef = myFirebaseRef.orderByChild("fullName").equalTo("gooner");

You can also get a range of nodes, by filtering with startAt and/or endAt instead of equalTo.

As Kato commented:

It looks like there is a superfluous level of children. The data is structured as saving-data/fireblog/<record id>/fluff/...actual data... and the fluff layer needs to be removed for those queries to work. You can't query children of children.

这篇关于如何从android中的firebase数据库检索特定的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆