【问题标题】:AutoCompleteTextView adapter doesn't update. Suggestion coming from an endpointAutoCompleteTextView 适配器不更新。来自端点的建议
【发布时间】: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


    【解决方案1】:

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

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

    现在一切正常!!

    【讨论】:

      猜你喜欢
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多