在Android中从Firebase实时数据库中检索数据 [英] Retrieving data from Firebase Realtime Database in Android

查看:131
本文介绍了在Android中从Firebase实时数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase和NoSQL的新手。我有一个Android演示版,其中有一个城市自动完成文本字段,我想在输入时从我的Firebase数据库填充城市。

 <$ 
瓜亚基尔:true,
Gualaceo:true,
基多:true,
Quevedo:true,
Cuenca:true,
Loja:true,
Ibarra:true,
Manta:true
}
}

这就是我到目前为止所做的。

如何从以字母开头的DB城市(从键盘输入)检索?如果我开始输入G,我想收到Guayaquil和Gualaceo。

如果我使用 orderByValue 总是返回一个空的快照。

如果我使用 orderByKey 返回整个列表。

 查询citiesQuery = databaseRef.child(cities)。startAt(input).orderByValue(); 
citiesQuery.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
List< String> cities = new ArrayList< String>(); $ (DataSnapshot postSnapshot:dataSnapshot.getChildren()){
cities.add(postSnapshot.getValue()。toString());
}

p>

注意:如果您可以推荐更好的数据结构,解决方案

解决方案

解决方案

解决方案

@NicholasChen已经发现了这个问题,但是您可以使用3.x SDK来实现:

DatabaseReference cities = databaseRef.child(cities)
Query cities = cities.orderByKey()。startAt(input).endAt(input +\\\)

  ; 
citiesQuery.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
列表与LT;字符串> cities = new ArrayList< String>(); (DataSnapshot postSnapshot:dataSnapshot.getChildren()){
cities.add(postSnapshot.getValue()。toString());



$ b $ p
$ b

从用户输入开始,到最后一个以用户输入,你得到所有匹配的项目

对于相对较短的项目列表Ryan的方法也将正常工作。但是上面的Firebase查询会过滤服务器端。



更新



p>

  DatabaseReference databaseRef = FirebaseDatabase.getInstance()。getReference(39714936); 

String input =G;

DatabaseReference cities = databaseRef.child(cities);
查询citiesQuery = cities.orderByKey()。startAt(input).endAt(input +\\\);
citiesQuery.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
List< String> cities = new ArrayList< String>(); $ (DataSnapshot postSnapshot:dataSnapshot.getChildren()){
cities.add(postSnapshot.getValue()。toString());
}
System.out的b
$ b .println(cities);
}

@Override
public void onCancelled(DatabaseError databaseError){


});

并印上:


true

true



随意测试我的数据库: https:/ /stackoverflow.firebaseio.com/39714936


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
    }
}

This is what I have so far.

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".

If I use orderByValue always returns an empty snapshot.

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 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

For relatively short lists of items Ryan's approach will also work fine. But the above Firebase query will filter server-side.

Update

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) {

        }
    });

And it printed:

true

true

So clearly matches two cities.

Feel free to test against my database: https://stackoverflow.firebaseio.com/39714936

这篇关于在Android中从Firebase实时数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆