我正在尝试在我的 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,它应该只检索具有该节点中所有字段的第二个节点。