如何在firebase中获取特定的子节点

时间:2017-03-30 07:19:57

标签: android firebase

大家好我是firebase的新手,我不知道这里的某些事情,我已经完成了数据上传,现在我想取一些用户数据,具体取决于bloodGroup用户这是我在JSON中的firebase数据< / p>

&#13;
&#13;
{
  "app_title" : "Realtime Database",
  "bgs" : {
    "AB+" : {
      "userId" : {
        "-KgSzL1qNweiG8OqX_Wk" : "-KgSzL1qNweiG8OqX_Wk",
        "-KgSzYx4F6O7f5_whsyO" : "-KgSzYx4F6O7f5_whsyO"
      }
    },
    "B+" : {
      "userId" : {
        "-KgSzk3M3ChX6dMajyrS" : "-KgSzk3M3ChX6dMajyrS"
      }
    }
  },
  "filledCities" : {
    "Chandigarh" : {
      "userId" : {
        "-KgSzL1qNweiG8OqX_Wk" : "-KgSzL1qNweiG8OqX_Wk",
        "-KgSzYx4F6O7f5_whsyO" : "-KgSzYx4F6O7f5_whsyO"
      }
    },
    "Shimla" : {
      "userId" : {
        "-KgSzk3M3ChX6dMajyrS" : "-KgSzk3M3ChX6dMajyrS"
      }
    }
  },
  "user" : {
    "-KgSzL1qNweiG8OqX_Wk" : {
      "email" : "cahkal",
      "name" : "rajt",
      "userBloodGroup" : "AB+",
      "userCity" : "Chandigarh",
      "userDob" : "ahaka",
      "userState" : "Chandigarh"
    },
    "-KgSzYx4F6O7f5_whsyO" : {
      "email" : "sgjklli",
      "name" : "sim",
      "userBloodGroup" : "AB+",
      "userCity" : "Chandigarh",
      "userDob" : "afjopitc",
      "userState" : "Dadra and Nagar Haveli"
    },
    "-KgSzk3M3ChX6dMajyrS" : {
      "email" : "abznzk",
      "name" : "rajat",
      "userBloodGroup" : "B+",
      "userCity" : "Shimla",
      "userDob" : "bMzvsk",
      "userState" : "Chandigarh"
    }
  }
}
&#13;
&#13;
&#13;

没有我想要的是我想从&#34; bgs&#34;中获取其血型等于AB +的userId。节点,然后使用所有那些userId,我想从&#34;用户&#34;中获取用户信息。节点

这是我在活动中的Java代码请帮助我,如果你能告诉我我的firebase数据结构是否适合使用我将非常感谢你们所有人都提前感谢你们:)

        datashow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("inside click", "onClick: ");
            FirebaseDatabase.getInstance().getReference("bgs").child("userId").orderByChild("AB+").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    if (user == null) {
                        Log.e(TAG, "User data is null!");
                        return;
                    }
                    if(dataSnapshot.exists()){
                        Iterable<DataSnapshot > children = dataSnapshot.getChildren();
                    for(DataSnapshot child : children)
                    {
                        Map<String , String> map = (Map)child.getValue();
                        Log.i("inside", "onDataChange: " + map);
                       name  = map.get("name");
                      email = map.get("email");
                       dateof = map.get("userDob");
                       showData(name , email  , dateof );
                  }

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });

0 个答案:

没有答案