Search code examples
androidkotlinandroid-filterable

Unable to filter AutoCompleteTextView after settings it's value to ""


I have an AutoCompleteTextView in my Android App, i have made a custom Adapter for it's layout and it's filter.

The issue is that the filter works fine till i cancel all i write in the AutoCompleteTextView, once i clear it the list from the constructor become empty and there isn't any item to filter and it's happen only when i empty the input value.

By default when the input is empty i would show the whole list of items the user can choose.

Here is my custom Adapter:

class FornitoriAdapter(context: Context, fornitori: MutableList<Fornitori> = mutableListOf(), resource: Int) :
    ArrayAdapter<Fornitori>(context, resource), Filterable {

    private var filteredList: MutableList<Fornitori> = mutableListOf()

    init {
        filteredList = fornitori.toMutableList()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val row: View
        val holder: ViewHolder

        if (convertView == null) {
            val inflater = LayoutInflater.from(context)
            row = inflater.inflate(R.layout.list_item, parent, false)
            holder = ViewHolder(row)
            row.tag = holder
        } else {
            row = convertView
            holder = row.tag as ViewHolder
        }

        val fornitore: Fornitori = filteredList[position]

        holder.descrText.text = fornitore.desc
        holder.codeText.text = fornitore.cod

        return row
    }

    override fun getCount(): Int {
        return filteredList.size
    }

    override fun getItem(position: Int): Fornitori {
        return filteredList[position]
    }

    private class ViewHolder constructor(row: View) {
        val descrText: TextView = row.findViewById(R.id.description)
        val codeText: TextView = row.findViewById(R.id.code)
    }

    override fun getFilter(): Filter {
        return customFilter
    }

    private val customFilter = object : Filter() {
        override fun convertResultToString(resultValue: Any?): CharSequence {
            return (resultValue as Fornitori).desc
        }

        override fun performFiltering(constraint: CharSequence?): FilterResults {
            return if (constraint != null) {
                filteredList.clear()
                fornitori.forEach { // HERE FORNITORI IF I EMPTY THE INPUT BECOME LENGTH 0
                    if (it.cod.equals(constraint.toString(), ignoreCase = true) || it.desc.toLowerCase(
                            Locale.ROOT
                        ).startsWith(
                            constraint.toString().toLowerCase(
                                Locale.ROOT
                            )
                        )
                    ) {
                        filteredList.add(it)
                    }
                }
                val filterResults = FilterResults()
                filterResults.values = filteredList
                filterResults.count = filteredList.size
                filterResults
            }else {
                filteredList.clear()
                filteredList = fornitori
                val filterResults = FilterResults()
                filterResults.values = filteredList
                filterResults.count = filteredList.size
                filterResults
            }
        }

        override fun publishResults(constraint: CharSequence?, filterResults: FilterResults) {
            val filterList = filterResults.values as? MutableList<*>
            if (filterResults.count > 0) {
                filterList?.forEach {
                    add(it as Fornitori)
                }.also {
                    notifyDataSetChanged()
                }
            }
        }

    }

}

So once i start to filter or choose one of the shown items all works okay, but once i cancel the input value i'm unable to get any value from autocomplete as fornitori is set to null, like it's list has been cleared but i actually clear only the filtered list..


Solution

  • The issue comes from you first setting

    filteredList = fornitori
    

    and then calling

    filteredList.clear()
    

    As you have set filteredList to refer to fornitori then by clearing filteredList you are also clearing fornitori. So instead of just reassigning the variable, you should actually copy fornitori into filteredList.


    Kotlin actually doesn't have a copy function for lists, but something like this is the most clear in my opinion:

    filteredList.clear()
    filteredList.addAll(fornitori)