SearchView suggestionsAdapter.changeCursor() doesn't work outside of onQueryTextChange()

41 views Asked by At

I'm trying to add suggestions to my SearchView. To do that I'm using the following CursorAdapter as suggestionsAdapter for my SearchView:

cursorAdapter = SimpleCursorAdapter(
    context,
    R.layout.item_search_suggestion,
    null,
    arrayOf(SearchManager.SUGGEST_COLUMN_TEXT_1),
    intArrayOf(R.id.search_suggestion_text),
    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
)

Then (to find appropriate suggestions) on SearchView query change I call ViewModel method to parse data from the server:

binding.eventCitySearchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
    override fun onQueryTextSubmit(query: String?): Boolean {
        return false
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        viewModel.handleUserIntent(CreateEventIntent.GetCitySuggestions(newText ?: ""))
        return true
    }

})

When my ViewModel successfuly parses suggestions I update my cursorAdapter:

private fun updateCitySuggestions(suggestions: List<String>) {
    val cursor = MatrixCursor(arrayOf(BaseColumns._ID, 
    SearchManager.SUGGEST_COLUMN_TEXT_1))
    suggestions.forEachIndexed { index, suggestion ->
        cursor.addRow(arrayOf(index, suggestion))
    }
    cursorAdapter.changeCursor(cursor)
}

The problem is it doesn't work. When I type something in the SearchView no suggestions appear. I debugged the code and know that suggestions in updateCitySuggestions() are not empty. So, the problem is not in the ViewModel. I also know that if I don't use ViewModel and call changeCursor() inside the onQueryTextChange() using mock data everything works fine.

0

There are 0 answers