如何也以小写字母搜索Firebase文本? [英] How to search Firebase text by lower case also?

查看:62
本文介绍了如何也以小写字母搜索Firebase文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Firebase数据库用于我的Android应用程序.我在应用程序中实现了搜索功能.在我的数据库中,仅当我以大写字母"A"开始搜索时,才会显示每个以"A"开头的标题.当我输入小写字母"a"时,未显示任何结果.我也想启用搜索小写字母的变体形式.请帮帮我.谢谢你.这是我的搜索代码.

I am using Firebase database for my Android Application. I implemented search feature in the app. On my database, every title start with "A" is showing only when I started a search with Capital "A". When I entered lower case "a" it showing no results found. I want to enable search for lower case variation also. Help me, please. Thank you. Here is my search code.

    private void firebaseUserSearch(String searchText) {

    Toast.makeText(Idioms.this, "Started Search", Toast.LENGTH_LONG).show();

    Query firebaseSearchQuery = mUserDatabase.orderByChild("Title").startAt(searchText).endAt(searchText + "\uf8ff");

    FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(

            Blog.class,
            R.layout.bank_row,
            BlogViewHolder.class,
            firebaseSearchQuery
    ) {
        @Override
        protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {

            viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getExample());
        }
    };
    mResultList.setAdapter(firebaseRecyclerAdapter);
}

推荐答案

有两种方法可以解决此问题.第一个方法是使用方法toLowerCase(),甚至从头开始就将项目名称以小写形式存储在数据库中.因此,如果要以小写字母搜索名称,将可以很好地工作.然后,如果您想在活动中不使用名称,并且要大写第一个字母,则只需使用以下代码行:

There are two ways in which you can solve this problem. The first one would be to store the name of your items in your database in lowercase even from the beginning by using the method toLowerCase(). So if you want to search the names by lower case will work perfectly fine. Then if you want to dispay the names in your activity and you want to capitalize the first letter, then just use the following line of code:

String firstLetterCapital = lowerCaseItemName.substring(0, 1).toUpperCase() + lowerCaseItemName.substring(1);

因此,如果使用上面的代码拥有名称为item的商品,则结果将为:Item.

So if you you have an item that has the name of item, using the above code, the result will be: Item.

第二种方法是将商品的小写名称存储为商品的属性,如下所示:

And the second approach would be to store the lowercase name of your items as a property of your item like this:

Firebase-root
    |
    --- items
          |
          --- ItemIdOne
                |
                --- itemName: Item
                |
                --- itemToLowerCase: item

然后要创建搜索,可以使用如下查询:

Then to create the search you can use a query that looks like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference itemsRef = rootRef.child("items");
Query query = itemsRef.orderByChild("itemToLowerCase").startAt(itemName.toLowerCase());

这篇关于如何也以小写字母搜索Firebase文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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