Passing JSON to hits widget

android
instantsearch
backend
#1

Hi,
I’m using a custom search bar and passing the query on to algolia search in NodeJS by index.search(), I am then returning the JSON result to android studio where I want to display the results using the hits widget.
My question is how do I parse the JSONArray result to the hits widget in android studio?
Thanks

#2

Hi @vimadan, thanks for your question!

With InstantSearch, you don’t have to pass manually the response to widgets. You should rather initialize InstantSearch with your credentials, connect it to Widgets, and call InstantSearch.search(): it will trigger a search and then automatically pass the response to your Hits.


The code will be quite straightforward:

       // YourActivity.java's onCreate
        searcher = Searcher.create(ALGOLIA_APP_ID, ALGOLIA_SEARCH_API_KEY, ALGOLIA_INDEX_NAME);
        helper = new InstantSearch(this, searcher);
        helper.search();
<!-- activity_your.xml -->
<!-- [...] Top of Your layout, including a ViewGroup container [...] -->
<com.algolia.instantsearch.ui.views.Hits
        android:id="@+id/hits"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        algolia:itemLayout="@layout/hits_item"/>
<!-- [...] Bottom of Your layout [...] -->
<!-- hits_item.xml -->
<layout>
    <TextView
            android:id="@+id/product_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            algolia:attribute='@{"name"}'/>
</layout>

You can have a look at the Getting started Guide for step-by-step instructions to build a simple search interface with InstantSearch Android :slight_smile:

#3

I understand that under normal circumstances I do not need to call hits explicitly. However I created my own search bar. I’m not using InstantSearch.search(). I am using regular search in NodeJS and getting a JSONArray as an output. Is there a way I can explicitly call hits with this JSONArray