14

Why the onQueryTextSubmit method in SearchView is processed twice? I need one result, how can I do it?

This is my code:

 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if (query != null)
                audioRequest(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    return true;
}
Igor Lobanov
  • 345
  • 3
  • 12

7 Answers7

29

You can use the following code to prevent onQueryTextSubmit from getting executed twice:

searchView.clearFocus();

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
8

It generates two outputs when entered from the keyboard on the emulator but it creates single output when clicked on the search button on the keypad of the emulator. So, I think you should not worry about this. On all mobile phones or tablets, this error might not going to happen.

But, searchView.clearFocus(); also works here.

user132262
  • 81
  • 1
  • 1
2

SearchView has the following OnEditorActionListener on Android API 28:

private final OnEditorActionListener mOnEditorActionListener = new OnEditorActionListener() {

    /**
     * Called when the input method default action key is pressed.
     */
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        onSubmitQuery();
        return true;
    }
};

And in my debugger, I see that onEditorAction is called with both KeyEvent.action == KeyEvent.ACTION_DOWN and then KeyEvent.action == ACTION_UP.

This seems like a bug in SearchView.

Heath Borders
  • 30,998
  • 16
  • 147
  • 256
-1

Just a shot. Since you're handling the search by yourself, try returning true instead of false.

@Override
public boolean onQueryTextSubmit(String query) {
    if (query != null)
        audioRequest(query);
    return true;
}

http://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html

Jodevan
  • 732
  • 1
  • 6
  • 20
-1

Want to search more than 1 word?

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) {return false; }

        @Override
        public boolean onQueryTextChange(String newText) {


            newText = newText.toLowerCase();

            final ArrayList<DictObjectModel> filteredList = new ArrayList<DictObjectModel>();

            for (int i = 0; i < wordcombimelist.size(); i++) {

                final String text = wordcombimelist.get(i).toLowerCase();
                if (text.contains(newText)) {

                    filteredList.add(new DictObjectModel(wordcombimelist.get(i),meancombimelist.get(i)));
                }
            }
            adapter = new CustomAdapter(filteredList);
            recyclerView.setAdapter(adapter);


            return true;
        }
    });
-2

try to call your method after the text changed like this

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText != null)
                audioRequest(newText);
            return false;
        }
    });
Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
-2

For everyone who is struggling with the same issue, here's what the documentation says about the onQueryTextSubmit(String query) method here :

The listener can override the standard behavior by returning true to indicate that it has handled the submit request.

So simply replace return false by return true :

@Override
public boolean onQueryTextSubmit(String query) {
    if (query != null)
        audioRequest(query);
    return true;
}
AndroWeed
  • 133
  • 2
  • 12