Search code examples
androidkotlinfilterandroid-arrayadaptersearchview

Filter for SearchView in Kotlin


Android environment

kotlin_version = '1.3.20'
compileSdkVersion 28

I have an adapter of type ArrayAdapter<String> which is represented as ListView in the app activity. How to get the filter for SearchView to work?

Activity.xml

<android.support.v7.widget.SearchView
        android:id="@+id/searchView">
        ...

</android.support.v7.widget.SearchView>


<ListView
        android:id="@+id/listView"
        ...
 />

Activity.kt

   ...

         searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

                    override fun onQueryTextChange(newText: String): Boolean {

                        adapter.filter.filter(newText)

                        return true
                    }

                    override fun onQueryTextSubmit(query: String): Boolean {
                        // task HERE
                        return false
                    }

                })

    ...

So the setOnQueryTextListener() is working and even the adapter is working properly, but I'm unable to get the filter working.


Solution

  • Solution

    The solution to this problem was rather in the code scope definition itself. For people who might stumble across similar situation, plz make sure to check the searchView.setOnQueryTextListener() & make sure to keep it out of any loops which you might be using for feeding the list for adapter. :)