Theprogrammingnoob
Theprogrammingnoob

Reputation: 127

Searchview not working with onclicklistener

The searchview is not working as I expected it to. When searching for a word using the searchview, the onclicklistener would not work, it would just click with no results. An example can be seen in the image below.(Image 2)

However when the searchview is empty the buttons work fine, it navigates to a new fragment.(Image 1)

I have been trying to find a way to solution, but I have no idea how to. I am using an Arrayadapter.

The images are at the bottom of the screen

MainActivity.kt for the searchview

lateinit var adapter:ArrayAdapter<String>
....

var user = arrayOf("Home","Dog","Cat","Hamster")
adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, user)

binding.categories.adapter = adapter

binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String): Boolean {
            if (user.contains(query)) {
                adapter.filter.filter(query)
            } else {
                Toast.makeText(this@MainActivity, "No Match found", Toast.LENGTH_LONG).show()
            }
            return false
        }
        override fun onQueryTextChange(newText: String): Boolean {
            adapter.filter.filter(newText)
            return false
        }
    })
    val home = adapter.getItemId(0)
    val cat = adapter.getItemId(1)
    val hamster = adapter.getItemId(2)
    val dog = adapter.getItemId(3)
    binding.categories.setOnItemClickListener { parent, view, position, ID ->
        when(ID){
            home ->{navController.navigate(R.id.action_global_home2)
            }
            cat -> {navController.navigate(R.id.action_global_cat) }
            hamster -> {navController.navigate(R.id.action_global_hamster)}
            dog -> {navController.navigate(R.id.action_global_dog)
            }
        }
    }

MainActivity.XML

<!--searchview-->
    <androidx.appcompat.widget.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="16dp"
        app:queryHint="Enter Search"
        android:background="@drawable/searchview_background"
        app:iconifiedByDefault="false"
        app:queryBackground="@android:color/transparent"


        >
    </androidx.appcompat.widget.SearchView>
    <!--Listview-->
    <ListView
        android:id="@+id/categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        tools:listitem="@layout/searchview_listitem"/>

Image 1

enter image description here

Image 2

enter image description here

Upvotes: 1

Views: 83

Answers (1)

user13561545
user13561545

Reputation:

You should try using a customadapter.

Upvotes: 1

Related Questions