Reputation: 445
the problem that I present is with respect to the event onQueryTextListener
, I declare it as it used it in java previously, but it does not show the log when I submit a text or when I change the text.
This is the code of my fragment:
Fragment_producto.kt
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val vG = inflater!!.inflate(R.layout.fragment_producto, container, false)
vG.recycler_producto.layoutManager = LinearLayoutManager(activity)
vG.recycler_producto.hasFixedSize()
vG.recycler_producto.adapter = Producto_Adapter(activity,this)
vG.fab_scan.onClick {
IntentIntegrator.forSupportFragment(this@Fragment_producto).initiateScan()
}
adapter = vG.recycler_producto.adapter as Producto_Adapter
metodos.attachSwipeCheck(vG.recycler_producto)
metodos.attachSwipeWrong(vG.recycler_producto)
setHasOptionsMenu(true)
return vG
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Log.i(TAG,"Llego a create optionsmenu")
activity.menuInflater.inflate(R.menu.menu_producto,menu)
/*val menuItem = menu.findItem(R.id.menu_search)
val search = menuItem.actionView as SearchView
searching(search)*/
super.onCreateOptionsMenu(menu, inflater)
}
override fun onPrepareOptionsMenu(menu: Menu) {
val menuItem = menu.findItem(R.id.menu_search)
val search = menuItem.actionView as SearchView
searching(search)
super.onPrepareOptionsMenu(menu)
}
private fun searching(search: SearchView){
search.onQueryTextListener {object: SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
Log.i(TAG,"Llego al querysubmit")
return false
}
override fun onQueryTextChange(newText: String): Boolean {
Log.i(TAG,"Llego al querytextchange")
return true
}
}}
}
Help me please.
Thanks
Upvotes: 5
Views: 7395
Reputation: 137
Check your xml code;
if your using androidx.appcompat.widget.SearchView you should use that object
setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String): Boolean {
return true
}
})
or change your searchView to standard one.
Upvotes: 1
Reputation: 957
Hi i had the same problem, my onQueryTextChange was not triggered... And after some search i found out that i was using androidx so i have to change my menu from : app:actionViewClass="android.widget.SearchView"
to app:actionViewClass="androidx.appcompat.widget.SearchView"
Also make sur that in your activity or fragment you importe the right SearchView: import androidx.appcompat.widget.SearchView
and not the android widget.
Finally if you want to add Focus listener, to know when the searchview is in use or not do this :
searchView.setOnQueryTextFocusChangeListener ( object : View.OnFocusChangeListener{
override fun onFocusChange(p0: View?, p1: Boolean) {
println("focus cahanged")
println(p1)
}
})
This return true is the searchview in focus and false if not.
Upvotes: 0
Reputation: 227
I had to also implement the listener for it to compile properly:
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener,
android.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
//
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
//
return false
}
})
Upvotes: 1
Reputation: 744
try changing your searching
method to this:
private fun searching(search: SearchView) {
search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
Log.i(TAG,"Llego al querysubmit")
return false
}
override fun onQueryTextChange(newText: String): Boolean {
Log.i(TAG,"Llego al querytextchange")
return true
}
})
}
I haven't tested it but your brackets seem iffy. I think there should be ()
instead of {}
The method you are using onQueryTextListener
(as compared to setOnQueryTextListener
in my code above) does not compile for me. Do you use any extension methods there? Maybe that method takes a function String->()
as an argument, so when you put the OnQueryTextListener
in the {}
you are just giving a lambda to the method as an argument that creates an object but never uses it.
Upvotes: 6