如何实现sqlite的“喜欢"android firebase-database中的查询 [英] How to implement sqlite "like" query in android firebase-database

查看:21
本文介绍了如何实现sqlite的“喜欢"android firebase-database中的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Firebase数据库中实现 sqlite like 查询.例如如果有10个条目abc1,abc2,abc3,..... abc10.用 abc +%" 进行的查询应显示所有10个条目.我尝试了 orderByChild(").equalTo(")以及 startAt("),但没有得到想要的结果.

I am trying to implement sqlite like query in firebase database. For e.g. If there are 10 entries abc1, abc2, abc3, ..... abc10. Query with abc+"%" should display all 10 entries. I tried orderByChild("").equalTo("") as well as startAt(""), but did not get the desired result.

尝试了链接,但仍然卡住了.请提出.

Tried this link, but still stuck. Kindly suggest.

推荐答案

在阅读了许多问题之后,我很失望地发现firebase没有实现sqlite的喜欢"查询,因此我决定解决这个问题.

Well after reading many questions i was disappointed to find out that firebase doesn't implement sqlite "like" query, so i decided to work around it.

我知道这个问题很旧,但是如果有人仍在努力寻找该问题的答案,我会找到另一种解决方案,那就是更精确地解决问题.

I know this question is old, but in case anyone still struggling with finding an answer to this problem, I have found an alternative solution, a work around to be more precise.

当应用程序启动时,将列表存储在内存中,并保留为原始数据以供您每次查询时使用,该列表仍将与firebase同步.这是我的操作方式.

when the application start, store the list in memory and keep it as a raw data to query from it everytime you need it, the list will still be synchronized with firebase..here is how i did it.

mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Loading data from firebase to the temporary listItem data
            ArrayList<ListItem> data = new ArrayList<>();
            for(DataSnapshot customList :  dataSnapshot.getChildren()){
                ListItem item = customList.getValue(ListItem.class);
                data.add(item);
            }
            listData.clear();
            listData.addAll(data);
            //listData above is my list that i use and re-use (clear and refull, rawListData is like my offline firebase database)
            rawListData = new ArrayList<>();
            rawListData.addAll(data);
            if(firstRun){
                firstRun=false;
                myAdapter = new MyAdapter(MainActivity.this, listData);
                recView.setAdapter(myAdapter);
                if(!isBind){
                    runService();
                }
            }else{
                myAdapter.notifyDataSetChanged();
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(MainActivity.this, "Database loading error", Toast.LENGTH_SHORT).show();
        }
    });

让我说我想在我的数据中搜索其中包含单词"blues"的标题..我只是调用此线程> filterMusicType("blues");

let's say I want to search in my data about a title that contains the word "blues" in it..i just call this thread > filterMusicType("blues");

    public void filterMusicType(String type){

    //Notify RecyclerView that listData was cleared.
    int currentSize = listData.size();
    listData.clear();
    myAdapter.notifyItemRangeRemoved(0, currentSize);

    //Filter data based on music type
    for(ListItem item : rawListData){
        String station = item.getTitle().toLowerCase();
        if(station.contains(type)){
            listData.add(item);
        }
    }

    //Notify RecyclerView that listData was laoded with new Data.
    myAdapter.notifyItemRangeInserted(0, listData.size());
    myAdapter.notifyDataSetChanged();
}

这篇关于如何实现sqlite的“喜欢"android firebase-database中的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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