自动完成文本视图适配器不会更新。来自终结点的建议



我有一个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动态更新适配器的正确方法是什么?

所以,在尝试了很多之后,我最终完成了这篇文章:

https://www.truiton.com/2018/06/android-autocompletetextview-suggestions-from-webservice-call/

class AutoSuggestAdapter(context: Context, resource: Int) : ArrayAdapter<String>(context, resource), Filterable {
private val mlistData: MutableList<String>
init {
mlistData = ArrayList()
}
fun setData(list: List<String>) {
mlistData.clear()
mlistData.addAll(list)
}
override fun getCount(): Int {
return mlistData.size
}
override fun getItem(position: Int): String? {
return mlistData[position]
}
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null) {
filterResults.values = mlistData
filterResults.count = mlistData.size
}
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results != null && results.count > 0) {
notifyDataSetChanged()
} else {
notifyDataSetInvalidated()
}
}
}
}
}
//Observer
mainModel.autoCompleteBYOSResult.observe(viewLifecycleOwner, Observer {
val temp: ArrayList<String> = ArrayList()
it.success?.forEach { temp.add(it.name) }
autoSuggestAdapter.setData(temp);
autoSuggestAdapter.notifyDataSetChanged();
})

//onCreate
autoSuggestAdapter = AutoSuggestAdapter(context!!, android.R.layout.simple_dropdown_item_1line)
et_item_name.threshold = 2
et_item_name.setAdapter(autoSuggestAdapter)
et_item_name.doOnTextChanged { text, start, count, after ->
handler.removeMessages(TRIGGER_AUTO_COMPLETE)
handler.sendEmptyMessageDelayed(TRIGGER_AUTO_COMPLETE, AUTO_COMPLETE_DELAY)
}
handler = object : Handler() {
override fun handleMessage(msg: Message?) {
if (msg?.what == TRIGGER_AUTO_COMPLETE) {
if (et_item_name.text.trim().length > 1) {
mainModel.getProductsAutoCompleteResults(ProductAutoCompleteRequest(10, et_item_name.text.trim().toString(), "SOME_ID"))
}
}
}
}

现在一切正常!!

最新更新