AutoCompleteTextView适配器不会更新。来自端点的建议

问题描述 投票:0回答:1

我有一个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个mObject。 AutoCompleteTextView下面的下拉列表不会出现。

为AutoCompleteTextView动态更新适配器的正确方法是什么?

android kotlin autocomplete autocompletetextview
1个回答
0
投票

因此,在尝试了很多之后,我最终还是遵循了这篇文章:

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"))
                    }
                }
            }
        }

现在全部工作!

© www.soinside.com 2019 - 2024. All rights reserved.