OnQueryTextSubmit in SearchView handled twice in Android Java

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

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; } 
+7
source share
8 answers

You can use the following code to prevent the onQueryTextSubmit executing twice:

searchView.clearFocus();

+16
source

It generates two outputs when entering from the keyboard on the emulator, but each time you press the search button on the emulator keyboard, one output is created. So, I think you should not worry about that. On all mobile phones or tablets, this error may not occur.

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

+2
source

Just a shot. Since you are processing the search 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

0
source

For anyone struggling with the same problem, here is what the documentation on the onQueryTextSubmit(String query) method onQueryTextSubmit(String query) here :

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

So just replace return false with return true :

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

Want to find more than one word?

searchView.setOnQueryTextListener (new SearchView.OnQueryTextListener () {@Override public boolean onQueryTextSubmit (query string) {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; } }); 
0
source

you should do an empty searchview after calling the audioRequest method. change your code as below:

 public boolean onQueryTextSubmit(String query) { if (query != null) audioRequest(query); searchView.setQuery(null,false); return true; } 

hope this helps you!

0
source

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 both with KeyEvent.action == KeyEvent.ACTION_DOWN and with KeyEvent.action == ACTION_UP .

This seems like an error in SearchView .

0
source

try calling your method after changing the text as follows

 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; } }); 
-1
source

Source: https://habr.com/ru/post/1237863/


All Articles