0

I am trying to convert below code in Jetpack compose UI,

    searchResultsView = findViewById<SearchResultsView>.(R.id.search_results_view).apply {
        initialize(
            SearchResultsView.Configuration(CommonSearchViewConfiguration(DistanceUnitType.IMPERIAL))
        )
        isVisible = false
    }


    searchEngineUiAdapter = SearchEngineUiAdapter(
        view = searchResultsView,
        searchEngine = searchEngine,
        offlineSearchEngine = offlineSearchEngine,
    )

I Need to pass searchResultView in our function SearchEngineUiAdapter,

I Stuck here , because searchView is now AndroidView and it as a Compose component, so we can not passed to a function. I don’t know inject this searchResultsView into a function

below is final code

AndroidView(
        factory = { context ->
            SearchResultsView(context).apply {
                initialize(
                    SearchResultsView.Configuration(
                        CommonSearchViewConfiguration(
                            DistanceUnitType.IMPERIAL
                        )
                    )
                )
            }
        },
        modifier = Modifier
            .fillMaxWidth()
            .background(color = Color.Blue)
Jitendra Prajapati
  • 1,002
  • 1
  • 10
  • 33

1 Answers1

0

In Jetpack Compose, you provide the data to the composable function to display, not the other way around. You should initialize and get your search results in a viewModel outside and then pass that data for display to the search results composable.

val searchResults = searchViewModel.getSearchResults()
AndroidView(
    factory = { context ->
        SearchResultsView(searchResults)
        }
    },
    modifier = Modifier
        .fillMaxWidth()
        .background(color = Color.Blue)
RufusInZen
  • 2,119
  • 1
  • 19
  • 26