Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView.onQueryTextListener does not work in Fragment (Kotlin)

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

like image 398
Russbell G.H Avatar asked Feb 09 '18 19:02

Russbell G.H


1 Answers

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.

like image 136
Hendrik Marx Avatar answered Sep 20 '22 23:09

Hendrik Marx