【发布时间】:2020-06-28 18:18:13
【问题描述】:
我有一个 AutoCompleteTextView (et_item_name),其数据源来自端点。这是设置初始适配器并在我们从端点接收到数据后重新加载它的代码。
productSuggestions = ArrayList()
mSearchSuggestionsAdapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, productSuggestions)
et_item_name.setAdapter(mSearchSuggestionsAdapter)
et_item_name.threshold = 1
et_item_name.doAfterTextChanged {
if (it.toString().trim().length <= 1) {
productSuggestions.clear()
mSearchSuggestionsAdapter.notifyDataSetChanged()
} else {
mainModel.getProductsAutoCompleteResults(ProductAutoCompleteRequest(10, it.toString(), "SOME_ID")) //this is an endpoint call, which returns fetched results
}
}
//Observer
mainModel.autoCompleteBYOSResult.observe(viewLifecycleOwner, Observer { //autoCompleteBYOSResult is MutableLiveData
productSuggestions.clear()
var temp: ArrayList<String> = ArrayList()
it.success?.forEach { temp.add(it.name) }
productSuggestions.addAll(temp) //this array has all correct values
mSearchSuggestionsAdapter.notifyDataSetChanged() //after this call, this adapter doesn't update, it still shows 0 mObjects when debugging
})
mSearchSuggestionsAdapter.notifyDataSetChanged() 不会更新适配器。在调试模式下它仍然显示 0 mObjects。 AutoCompleteTextView 下方的下拉菜单没有出现。
为 AutoCompleteTextView 动态更新适配器的正确方法是什么?
【问题讨论】:
标签: android kotlin autocomplete autocompletetextview