I have this code:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_search, menu)
val searchItem = menu.findItem(R.id.action_search)
val searchView = MenuItemCompat.getActionView(searchItem) as SearchView
//*** setOnQueryTextFocusChangeListener ***
searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener() {
override fun onFocusChange(v: View, hasFocus: Boolean) {
}
})
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
return false
}
override fun onQueryTextChange(searchQuery: String): Boolean {
adapter!!.filter(searchQuery.toString().trim { it <= ' ' })
tvListAnimal.invalidate()
return true
}
})
return true
}
The problem comes from this part:
searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener() {
override fun onFocusChange(v: View, hasFocus: Boolean) {
}
})
When I try to run with Android Studio, I get this message:
'This class does not have a constructor.'
I try to write like this:
searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener {
override fun onFocusChange(v: View, hasFocus: Boolean) {
}
})
It works but the app crashes, because I remove the parenthesis after View.OnFocusChangeListener
.
I have no idea how to complete this. Someone has an idea?