🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
UI libraries / InstantSearch Android / Widgets

About this widget

Hits is a view with helper components used to display a list of search results. Use HitsPaging if you want to use []”infinite scrolling”](/doc/guides/building-search-ui/ui-and-ux-patterns/infinite-scroll/android/) in your search.

To add Hits to your search experience, use these components:

  • Searcher: The Searcher that handles your searches.
  • T: A data class representing a search result.
  • HitsView<T>: The view that renders objects of type T.

If you want to search across more than one index, see Multi-index search.

If there are no hits, you should display a message to users and clear filters so they can start over.

Examples

You can use deserialize to convert raw hits into your own data class using the Serializable annotation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class MyActivity : AppCompatActivity() {

    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val connection = ConnectionHandler()
    val adapter = MovieAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        connection += searcher.connectHitsView(adapter) { response ->
            response.hits.deserialize(Movie.serializer())
        }
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connection.disconnect()
        searcher.cancel()
    }
}

@Serializable
data class Movie(
    val title: String
)

class MovieViewHolder(val view: TextView): RecyclerView.ViewHolder(view) {

    fun bind(data: Movie) {
        view.text = data.title
    }
}

class MovieAdapter : RecyclerView.Adapter<MovieViewHolder>(), HitsView<Movie> {

    private var movies: List<Movie> = listOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
        return MovieViewHolder(TextView(parent.context))
    }

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        val movie = movies[position]

        holder.bind(movie)
    }

    override fun setHits(hits: List<Movie>) {
        movies = hits
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return movies.size
    }
} 

Compose UI

InstantSearch provides the LoadingState as a state model, which is an implementation of the LoadingView interface. You need to connect LoadingState to the LoadingConnector or LoadingViewModel like any other LoadingView implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val hitsState = HitsState<Movie>()
    val connections = ConnectionHandler()  

    init {
        connections += searcher.connectHitsView(hitsState) { 
            it.hits.deserialize(Movie.serializer()) 
        }
    }  

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LazyColumn {
                items(hitsState.hits) { movie ->
                    Text(movie.title)
                }
            }
        }
        searcher.searchAsync()
    }  

    override fun onDestroy() {
        super.onDestroy()
        connections.disconnect()
        searcher.cancel()
    }
}

Parameters

Parameter Description
transform
type: (ResponseSearch) -> List<T>
Required

A function transforming the search response into a list of results of your class T.

1
2
3
4
val transform: (ResponseSearch) -> List<Movie> = { response ->
    response.hits.map { hit -> Movie(hit.json.getPrimitive("title").content) }
}
searcher.connectHitsView(adapter, transform)
Did you find this page helpful?