Home > Software design >  AutoCompleteTextView - only one item at the dropdown menu after configuration changes
AutoCompleteTextView - only one item at the dropdown menu after configuration changes

Time:10-26

I set up AutoCompleteTextView like this:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding.viewModel = viewModel
    
    initSortingAutoCompleteTextView()
}

private fun initSortingAutoCompleteTextView() {
    val adapterSorting = ArrayAdapter(
        activityContext,
        android.R.layout.simple_spinner_dropdown_item,
        sortingValues.map { it.title }
    )
    with(binding.sortingSelectAutocompleteText) {
        setAdapter(adapterSorting )
    }
}

It works fine:

enter image description here

But after configuration changes (orientation change for example) there is only one item:

enter image description here

What is the issue and how to fix it?

CodePudding user response:

It's the implicit behavior of the AutoCompleteTextView/ArrayAdapter.

It has all of the items, but since it is trying to auto-complete it shows all the items that fit "Price" string.

One solution is to just disable the filtering.

class NonFilterArrayAdapter<T>(context: Context, @LayoutRes resource: Int, objects: List<T>) : ArrayAdapter<T>(context, resource, objects) {

    override fun getFilter() = NonFilter()

    class NonFilter : Filter() {
        override fun performFiltering(constraint: CharSequence?) = FilterResults()

        override fun publishResults(constraint: CharSequence?, results: FilterResults?) = Unit
    }
}
  • Related