androidtransformalgoliainstantsearch

Is there a transformItems equivalent in the Android Java Libraries for Algolia?


I have a use case where i would like to render the image associated with the hits returned from an Algolia search using the Algolia Java library for Android. I am currently developing on Pie . Here is what i am doing :

  1. I use com.algolia.instantsearch.core.helpers.Searcher
  2. I bind the results to a fragment which has a layout with the algolia attributes for images

    <ImageView
         algolia:attribute='@{"image_url"}'
    >
    
  3. The trouble is that the response JSON only stores the name of the JPG image which needs to be displayed. I need to dynamically add the base site URL and some more path specifiers . I tried doing something like this

    algolia:attribute='https://somedomain.com/somepath1/ProductImages/@{"BaseProductId"}/thumbnails/@{"image_url"}

    But that was not accepted.

  4. I am looking for a way to transform the results so i can build the complete URL and place it in the image_url and then use it in the layout as stated in the first code fragment.

Is there any way to do it ?


Solution

  • I solved it by adding a listener and updating the hits object as seen below.

     searcher.registerResultListener(new AlgoliaResultsListener() {
                     @Override
                     public void onResults(@NonNull SearchResults results, boolean isLoadingMore) {
                         for (int i=0;i<results.hits.length();i++){
                             try {
                                    JSONObject obj = results.hits.getJSONObject(i);
                                    String image_url_file = obj.getString("image_url");
                                    String base_product_id = obj.getString("BaseProductId");
                                    String full_image_path = "https://somedomain.com/somPath/ProductImages/"+base_product_id+"/Original/"+image_url_file;
                                    results.hits.getJSONObject(i).put("image_url",full_image_path);
                             }catch(Exception exx){
    
                             }
                         }
                     }
                 }
            );