The onQueryTextSubmit in SearchView is processed twice in Android Java

百般思念 提交于 2020-08-04 04:14:51

问题


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;
}

回答1:


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

searchView.clearFocus();




回答2:


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.




回答3:


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



回答4:


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.




回答5:


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




回答6:


you must make empty searchview after calling 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 help you!!




回答7:


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



回答8:


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;
}


来源:https://stackoverflow.com/questions/34207670/the-onquerytextsubmit-in-searchview-is-processed-twice-in-android-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!