1

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);
}
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Rahul Das
  • 59
  • 1
  • 8

1 Answers1

10

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

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());
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193