Reputation: 427
I am trying to insert in my View an AutoCompleteTextView, of which I don't immediately know the list to filter in , because it comes to me from an API call
I have a control when you go to type in the text field of the AutoCompleteTextView It calls a searchListMiseaJour function that makes an API call to fetch a list of Communes by going to modify listOfPostsSearch
override fun onTextChanged(
s: CharSequence, start: Int, before: Int, count: Int
) {
myActivity.searchListMiseaJour(s.toString())
}
So I have an observe that when listOfPostsSearch changes it calls the showListSearch function where I go to fill AutoCompleteTextView
communes.listOfPostsSearch.observe(this) { list ->
showListSearch()
}
fun showListSearch
{
communes.listOfPostsSearch.value?.let {
val resultCommunes = ArrayList<String>()
for (commune in it) {
resultCommunes.add("${commune.codePostal} - ${commune.nom}")
}
binding.rvAddClientAdresse.findViewWithTag<AutoCompleteTextView>("commune").threshold = 1
binding.rvAddClientAdresse.findViewWithTag<AutoCompleteTextView>("commune").setAdapter(adapterResultCommunes)
adapterResultCommunes.setNotifyOnChange(true)
}
}
Unfortunately, it doesn't work well for me, because if I write three characters, it makes the API call, gets to the Adapter, but then it doesn't show the list with the suggestions . If I write another character, it shows me the list (with the previous characters) but after a few moments in the list the results change with the new ones. The same thing happens if I delete a character, it shows the list, which after a few moments changes
There is something I don't quite understand in the steps to fill with the AutoCompleteTextView adapter and then show the filter
adapterResultCommunes is valued, but the first time it is not shown and after that it has old values which then change with observe
Thanks
Upvotes: 0
Views: 34