RecyclerView Filter is not filtering (Kotlin)

456 Views Asked by At

I started the debugger and my breakpoints show that on the first startup the dataset is being transmitted as the adapter is being initialized, but as soon as I call the filter it doesn't have any data to work with and my recyclerview just stays as it was before.

All this code worked before, I just translated it from Java to Kotlin. I must have messed something up, but I can't find it.

Adapter Class

class ClothingListAdapterKt(
private val dataSetIn: MutableList<Clothing>,
private val listener: ClothingListAdapterKt.OnItemClickListener,
private val context: Context
) :
RecyclerView.Adapter<ClothingListAdapterKt.ViewHolder>(), Filterable {

private var lastPosition = -1
var dataSet = mutableListOf<Clothing>()
var dataSetFiltered = mutableListOf<Clothing>()

init {
    dataSet = dataSetIn
    dataSetFiltered = dataSet
}

override fun onBindViewHolder(viewHolder: ClothingListAdapterKt.ViewHolder, position: Int) {


    val currentClothing: Clothing = dataSetFiltered[position]

    // Get element from your dataset at this position and replace the
    // contents of the view with that element
    
    //SETTING MY VIEWS, ONLY COMMENTED OUT FOR THIS QUESTION

    }

    setAnimation(viewHolder.itemView, position);
}

override fun getFilter(): Filter {
    return object : Filter() {
        override fun performFiltering(constraint: CharSequence?): FilterResults? {

            val charString: String = constraint.toString()

            
            if (charString.isEmpty()) {
                dataSetFiltered.addAll(dataSet)
            } else {
                val dataSetTemp: MutableList<Clothing> = mutableListOf()
                for (row in dataSet) {

                    
                    if (row.name.lowercase().contains(charString.lowercase())) {
                        dataSetTemp.add(row)
                    }
                    
                }
                dataSetFiltered = dataSetTemp
            }
            val filterResults = FilterResults()
            filterResults.values = dataSetFiltered
            return filterResults
        }

        override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
            dataSetFiltered = results?.values as MutableList<Clothing>
            notifyDataSetChanged()
        }
    }
}

Adapter Init

recylcerViewClothing.adapter = ClothingListAdapterKt(clothing, listener, this.requireContext())

This is how I call the filter

adapter.filter.filter(searchTerm)    //searchTerm is a String passed by the constructor of the function it sits in
1

There are 1 best solutions below

1
On

this is my filter and It works properly

private val filter = object : Filter() {
    override fun performFiltering(constraint: CharSequence?): FilterResults {
        val result = FilterResults()
        val suggestions: MutableList<ShopAddressProvince> = mutableListOf()

        if (constraint != null) {
            suggestions.clear()
            val filterPattern = constraint.toString().lowercase()
            for (item in list) {
                if (item.text.lowercase().contains(filterPattern)) {
                    suggestions.add(item)
                }
            }
            result.values = suggestions
            result.count = suggestions.size
        }

        return result
    }

and check publish results

override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
        clear()
        if (results != null && results.count > 0) {
            addAll(results.values as MutableList<ShopAddressProvince>)
        } else {
            addAll(list)
        }
        notifyDataSetChanged()
    }