久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 Android 中的 Firebase 實時數據庫中檢索數據

Retrieving data from Firebase Realtime Database in Android(從 Android 中的 Firebase 實時數據庫中檢索數據)
本文介紹了從 Android 中的 Firebase 實時數據庫中檢索數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現 IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 日韩中文一区二区三区 | 久久一区视频 | 黄色成人免费看 | 午夜专区| 一区不卡在线观看 | 欧美日本一区二区 | 黄色日本视频 | 欧美老少妇一级特黄一片 | 天天在线操 | 久久伊人影院 | 日韩中文字幕免费 | 国产精品网页 | 一区二区在线不卡 | 天天精品在线 | a免费观看 | 欧美 日韩 亚洲91麻豆精品 | 免费看黄色小视频 | 成人在线观看免费视频 | 久热久热 | 黄色中文字幕 | 亚洲精品电影 | 精品国产1区2区3区 在线国产视频 | 久久99精品久久久水蜜桃 | 国产精品视频一区二区三区 | 中文字幕 欧美 日韩 | 亚洲视频中文字幕 | 亚洲精品在线免费观看视频 | 中国av在线免费观看 | 九九热在线免费观看 | 成人精品久久 | 亚洲欧美日韩国产综合 | 久久91| 欧美日韩精品免费观看 | 中文字幕日本一区二区 | 久久久久九九九女人毛片 | 国产精品s色 | 日韩av啪啪网站大全免费观看 | 免费观看成人性生生活片 | 久久久国产亚洲精品 | 欧美日韩在线综合 | 亚洲 欧美 日韩 在线 |