RecyclerView Filter is not filtering (Kotlin)

37
September 19, 2021, at 7:00 PM

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
Answer 1

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()
    }
READ ALSO
Pulling a docker image to client site [closed]

Pulling a docker image to client site [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post

71
How to automatic update the data in QTableWidget from mysql?

How to automatic update the data in QTableWidget from mysql?

I'm working for a simple project and I need a way to update the QTablewidget below automatically if there's new data inserted or deleted

65
React web based app connect to physcal hardware devices

React web based app connect to physcal hardware devices

I don't know is the right place or not to ask this questionI have a web-based inventory application that is running on Ipad

49
Postgresql&#39;s QueryExecutor hanging indefinitely at `receiveChar()` for query which has successfully completed

Postgresql's QueryExecutor hanging indefinitely at `receiveChar()` for query which has successfully completed

I'm facing a weird issue where Postgres seemingly isn't sending a reply to my client code even after successfully completing the queryAs such, it hangs around indefinitely in the blocking receiveChar() call waiting for something that never arrives

74