如何从android中的firebase数据库中检索特定节点

goo*_*ner 15 android firebase firebase-realtime-database

我想在我的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);
                Map<String, User> users = new HashMap<String, User>();
                users.put(name, userName);                    
              myFirebaseRef.push().setValue(users);
Run Code Online (Sandbox Code Playgroud)

这样创建数据库格式

{
      "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"
              }
            }
          }
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

我想从firebase中检索数据,这样,如果我在我的应用搜索框中输入"全名",它应该检索该特定节点,以便我可以在Listview中填充该信息.

这是我试图检索的方式,

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");
                    }

                    }
Run Code Online (Sandbox Code Playgroud)

但它返回所有节点,

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
Run Code Online (Sandbox Code Playgroud)

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

Fra*_*len 23

您在此行中创建了一个查询:

Query queryRef = myFirebaseRef.orderByChild("fullName");
Run Code Online (Sandbox Code Playgroud)

就像那样,查询通过它们的fullName值对子节点进行排序.但它还没有限制返回的子节点.

要限制节点,还必须进行过滤.例如:

Query queryRef = myFirebaseRef.orderByChild("fullName").equalTo("gooner");
Run Code Online (Sandbox Code Playgroud)

您还可以通过使用startAt和/或endAt代替来获取一系列节点equalTo.

正如加藤评论的那样:

看起来有多余的孩子.数据结构为saving-data/fireblog/<record id>/fluff/...actual data...并且需要移除绒毛层以使这些查询起作用.你不能查询孩子的孩子.