問題描述
我是 Firebase 和 NoSQL 的新手.我有一個 Android 演示,帶有一個城市自動完成文本字段,我想在輸入時從我的 Firebase 數據庫中填充我擁有的城市.
I'm new to Firebase and NoSQL. I have an Android Demo, with a City Autocomplete Text Field in which I want to populate the cities I have from my Firebase DB, while typing.
{ "cities":{
"Guayaquil":true,
"Gualaceo":true,
"Quito":true,
"Quevedo":true,
"Cuenca":true,
"Loja":true,
"Ibarra":true,
"Manta":true
}
}
這是我目前所擁有的.
如何從以字母開頭的數據庫城市中檢索(從鍵盤輸入)?如果我開始輸入G",我想收到Guayaquil"和Gualaceo".
How can I retrieve from the DB cities that start with a letter (input from keyboard)? If I start typing "G", I want to receive "Guayaquil" and "Gualaceo".
如果我使用 orderByValue
總是返回一個空快照.
If I use orderByValue
always returns an empty snapshot.
如果我使用 orderByKey
返回整個列表.
If I use orderByKey
return the whole list.
Query citiesQuery = databaseRef.child("cities").startAt(input).orderByValue();
citiesQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> cities = new ArrayList<String>();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
cities.add(postSnapshot.getValue().toString());
}
注意:如果您能推薦更好的數據結構,歡迎您.
Note: If you can recommend a better data structure, you're welcome.
推薦答案
@NicholasChen 已發現問題.但這是您使用 3.x SDK 實現的方式:
@NicholasChen has identified the problem. But here's the way you'd implement using the 3.x SDK:
DatabaseReference cities = databaseRef.child("cities")
Query citiesQuery = cities.orderByKey().startAt(input).endAt(input+"uf8ff");
citiesQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> cities = new ArrayList<String>();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
cities.add(postSnapshot.getValue().toString());
}
從用戶輸入開始,到以用戶輸入開頭的最后一個字符串結束,您將獲得所有匹配項
By starting at the user input and ending at the last string that starts with the user input, you get all matching items
對于相對較短的項目列表,Ryan 的方法也可以正常工作.但是上面的 Firebase 查詢會過濾服務器端.
For relatively short lists of items Ryan's approach will also work fine. But the above Firebase query will filter server-side.
我剛剛運行了這段代碼:
I just ran this code:
DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("39714936");
String input = "G";
DatabaseReference cities = databaseRef.child("cities");
Query citiesQuery = cities.orderByKey().startAt(input).endAt(input + "uf8ff");
citiesQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> cities = new ArrayList<String>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
cities.add(postSnapshot.getValue().toString());
}
System.out.println(cities);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
它打印出來了:
是的
是的
如此明顯地匹配兩個城市.
So clearly matches two cities.
隨意對我的數據庫進行測試:https://stackoverflow.firebaseio.com/39714936
Feel free to test against my database: https://stackoverflow.firebaseio.com/39714936
這篇關于從 Android 中的 Firebase 實時數據庫中檢索數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!