Alejandro Vargas
Alejandro Vargas

Reputation: 89

How to modify data in recyclerview by changing filter?

what I want to do is update the recyclerview information as I enter some text to filter information. I use "notifydatasetchange" but it does not show anything. The filtered information is ok.

This is my global list

var productList: MutableList<Product> = mutableListOf()

this is the adapter function which displays the first time ok

private fun buildProductAdapter(filter: String) {
        productList = Product().getProductsByFilter(filter)
        productListAdapter = ProductListAdapter(applicationContext, productList, cashOrCredit)
        rcvProducts.adapter = productListAdapter
    }

then, when the text has changed

private fun notifyProductListFilterChanged() {
    val filter: String = if (edtSearchProduct.text.toString() != "") edtSearchProduct.text.toString() else ""

    productList.clear()
    productList = Product().getProductsByFilter(filter)
    productListAdapter!!.notifyDataSetChanged()

}

But when I get to that function, it just does not display anything.

Upvotes: 0

Views: 248

Answers (1)

Dominykas Viecas
Dominykas Viecas

Reputation: 69

This happens because you change the reference of your variable to a new list

productList = Product().getProductsByFilter(filter)

while the adapter still holds the reference to the old one.

Instead, add your new items to the same list:

productList.addAll(Product().getProductsByFilter(filter))

Upvotes: 1

Related Questions