SearchView with no return on Android

0

I implemented a SearchView and put a Toast to see if it was working as soon as I typed something in the search. But Toast is not displayed, no error appears in Logcat. The quest is in an ActionBar. When I click on the search icon, it expands to be able to type however as soon as I press "enter" / "confirm" Toast does not appear.

This is the part where I'm calling Search

public void pesquisar(MenuItem item){

        searchView = (SearchView) item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(MainActivity.this, "ola", Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                            return false;
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);

        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.sair:
                this.finish();

            case R.id.sobre:
                return true;

            case R.id.cadastrar:
                Intent telaCadastro = new Intent(MainActivity.this, CadastroMaterias.class);
                startActivity(telaCadastro);

            case R.id.pesquisar:
                pesquisar(item);

            default:
                return super.onOptionsItemSelected(item);

        }
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <item android:id="@+id/sobre"
        android:title="Sobre"
        android:orderInCategory="100"
        app:showAsAction="never"/>

    <item android:id="@+id/sair"
        android:title="Sair"
        android:orderInCategory="150"
        app:showAsAction="never"/>

    <item android:id="@+id/cadastrar"
        android:title="Adicionar Materia"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_add"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/pesquisar"
        android:title="Pesquisar"
        android:orderInCategory="150"
        android:icon="@drawable/ic_search"
        app:showAsAction="always"
        app:actionViewClass="android.widget.SearchView"/>

    <item android:id="@+id/excluir"
        android:title="Excluir todos os registros"
        android:orderInCategory="200"
        android:icon="@drawable/ic_delete"
        app:showAsAction="ifRoom"/>

</menu>
    
asked by anonymous 30.05.2017 / 23:02

1 answer

0

Call the search or change the implementation of it to within onCreateOptionsMenu and leave the return of onQueryTextSubmit to true.

    
31.05.2017 / 21:55