从 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 实时数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)