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

标签 android firebase firebase-realtime-database

我正在尝试在我的 Android 应用程序中使用 Firebase。我正在关注 Saving 的文档和 retrieving , 但是教程中使用的示例数据库(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);

像这样创建数据库格式

{
      "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"
              }
            }
          }
        }
      }
    }

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

                    }

但它返回所有节点,

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

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

最佳答案

您正在这一行中创建一个查询:

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

这样,查询按子节点的 fullName 对子节点进行排序值(value)。但它还没有限制返回哪些子节点。

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

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

您还可以获得一系列节点,通过使用 startAt 进行过滤和/或 endAt而不是 equalTo .

正如 Kato 评论的那样:

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 - 如何从android中的firebase数据库中检索特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601663/

相关文章:

android - 在 firebase 上进行查询

android - 我可以在 Google Play 控制台的哪个位置查看我的应用的 Android 版本分布?

android - 如何在 Android Studio 上集成外部 SDK?

java - Android 将 View 转换到其真实类

firebase - 使用事务更新两个不同节点的值

firebase - 为什么将新的后代节点添加到正在监听的节点时会触发 Firebase 实时数据库 onChildChanged?

firebase - Firebase:激活只读和非实时模式以提高浏览器性能

firebase - 带有 Firebase 实时数据库和 futurebuilder 的 Gridview.builder

javascript - AngularFire 2 - FirebaseApp 与 FirebaseRef

java - Intent.ACTION_GET_CONTENT 和 Intent.ACTION_PICK 之间的区别