Search code examples
javaandroidfirebaseandroid-recyclerviewsearchbar

Sql Like query for Firebase Database Reference with MaterialSearchBar


This is my data where I want to search for specifically in products.about :

"products": {
    "10001": {
        "about": "Marble Chowki pair with intricate floral Painting by Handicrafts Paradise",
        "category": "handicrafts",
        "contact_vendor": "09171430513",
        "images": {
            "1001": {
                "url": "https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fchowki.jpg?alt=media&token=a74bf78e-f5ad-410c-aa28-9cbcacf386c4"
            },
            "1002": {
                "url": "https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fchowki1.jpg?alt=media&token=f91b064f-98fa-4504-91ff-ef56b43ab1fc"
            }
        },
        "shop_location": "Stall No. 56, Saras Mela, Patna",
        "vendor_address": "Rajasthan Handicraft And Textiles, Opposite Dada Bari, amer, Sagar Rd, Devisinghpura, Rajasthan 302028"
    },
    "10002": {
        "about": "Generic ITOS365 Handmade Vintage Dummy Gramophone Only For Home Décor",
        "category": "handicrafts",
        "contact_vendor": "01412630837",
        "images": {
            "1001": {
                "url": "https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fgramophone.jpg?alt=media&token=8a5cb8a1-4786-4d1b-b5cc-1072de9331de"
            },
            "1002": {
                "url": "https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fgramophone1.jpg?alt=media&token=6992be5f-2153-4cc5-89ef-1e36850ae21f"
            }
        },
        "shop_location": "Stall No. 56, Saras Mela, Patna",
        "vendor_address": "Near Rmgarh Turn, Amber Road, Sanganeer, Amber Road, Jaipur, Rajasthan 302002"
    }
}

And this is implementation for MaterialSearchBar that I am using :

searchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
        @Override
        public void onSearchStateChanged(boolean enabled) {
            if(enabled) {
                GridAutoFitLayoutManager layoutManager = new GridAutoFitLayoutManager(view.getContext(), 300);
                categories.setLayoutManager(layoutManager);
                categories.setNestedScrollingEnabled(false);

                searchListRecyclerAdapter = new ProductsRecyclerAdapter(searchList, view.getContext()) ;
                categories.setAdapter(searchListRecyclerAdapter);

            } else {
                onButtonClicked(MaterialSearchBar.BUTTON_BACK) ;
            }
        }

        @Override
        public void onSearchConfirmed(CharSequence text) {
       //HERE IS THE COMPLICATION

            searchList = CategoryActivity.feed_data() ;
            searchListRecyclerAdapter = new ProductsRecyclerAdapter(searchList, view.getContext()) ;
            categories.setAdapter(searchListRecyclerAdapter);
        }

        @Override
        public void onButtonClicked(int buttonCode) {
            if(buttonCode == MaterialSearchBar.BUTTON_BACK) {
                categoriesRecyclerAdapter = new CategoriesRecyclerAdapter(categoryList, view.getContext());
                LinearLayoutManager verticalLayoutManager = new LinearLayoutManager
                        (view.getContext(), LinearLayoutManager.VERTICAL, false);
                categories.setLayoutManager(verticalLayoutManager);
                categories.setNestedScrollingEnabled(false);
                categories.setAdapter(categoriesRecyclerAdapter);
            }
        }
    });
}

I would like to somehow search the Firebase database and populate the data in searchList to create the layout. I dont have the idea to how to implement it.

Here is the activity source code


Solution

  • Here is how I have to solve in the end. As I can somewhat compromise on performance but I needed my search result to be accurate, I used a for-each loop on all the products one by one and I searched every string that I wanted.

    Whichever object I needed I added them in ArrayList and in the end passed through feedData() function.

    I know this is not the best solution but I had to improvise this is not possible in firebase and this is as far as I can get for good results.