I am unable to navigate to the next fragment when there are characters typed in the searchview. For instance if I were to type 'Dog' in the searchview and select the Dog option from the Listview it would direct me back to the Homepage.
However if nothing is in the searchview, as in nothing is typed out in the searchview the navigation works fine. For instance, clicking on the Dog option from the Listview would navigate me to the Dog fragment.
I also can't seem to get the listview to collapse when not clicking on the searchview.
I have attached an image as a reference to the problem I am facing.
In the code below I am using a default ArrayAdapter
Searchview.kt
//searchview
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)
}
}
}
Searchview.xml
<androidx.drawerlayout.widget.DrawerLayout
.....
<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"/>
.....
<androidx.drawerlayout.widget.DrawerLayout