如何從android中的firebase數據庫檢索特定的節點



Answers

如果我們想要在剛剛從中檢索數據的同一個關鍵字下添加更多的節點呢? 例如:

"android" :{

        "saving-data" : {
          "fireblog" : {
            "-JiRtkpIFLVFNgmNBpMj" : {
              "Name" : {
                "birthDate" : "100",
                "fullName" : "Name",
                "healthCond" : "fyhft",
                "mob1" : "5855",
                "mob2" : "5858",
                "socialNumber" : "100"
"More Info" : {

                "status" : "100",
                "gender" : "Name",
                "gfg" : "fyhft",
                "fgfg" : "5855",
                "fgf" : "5858",
                "fgf" : "100"
              }
Question

我正在嘗試在我的android應用程序中使用Firebase。 我正在按照保存檢索的文檔,但教程中使用的示例數據庫(龍)具有不同於我的數據庫的結構。

這是我的代碼將數據推送到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,它應該只檢索該節點中的所有字段的第二個節點。




Links