【发布时间】:2015-02-19 08:32:43
【问题描述】:
我正在尝试在我的 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,它应该只检索具有该节点中所有字段的第二个节点。
【问题讨论】:
-
看来孩子的水平是多余的。数据的结构为
saving-data/fireblog/<record id>/fluff/...actual data...,需要删除fluff层才能使这些查询正常工作。您不能查询子项的子项。 -
@Kato ,是的,我注意到了这一点并修改了我的推送代码。查询现在有效。但是现在我在 If(Data not found ) 条件下面临的另一个问题。由于此查询一一遍历所有节点,我无法让 else{} 语句 中的代码正常工作。我考虑使用'queryRef.addListenerForSingleValueEvent',但查询返回所有节点
-
这听起来像是一个答案@kato。 :-)
-
@FrankvanPuffelen 随时在您的答案中添加注释,因为任何访问该问题的人都会引用该注释。
-
@FrankvanPuffelen,我有疑问。为什么相同的查询 (Query queryRef = myFirebaseRef.orderByChild("fullName").equalTo("gooner");) 在 queryRef.addChildEventListener() 和queryRef.addListenerForSingleValueEvent() ?第一个返回单个特定节点,第二个返回所有节点。
标签: android firebase firebase-realtime-database