Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string

后端 未结 13 842
盖世英雄少女心
盖世英雄少女心 2020-12-05 10:17

I am using Android 4.1.2. I have a SearchView widget on an ActionBar. Documentation on SearchView.OnQueryTextListener

相关标签:
13条回答
  • 2020-12-05 10:46

    As others have mentioned this behavior is intentional. I gave up on a solution for OnQueryChangeListener and decided to workaround by implementing OnEditorActionListener on the SearchView's EditText, which you can get a handle to using R.id.search_src_text. As long as you setImeOptions of the SearchView to EditorInfo.IME_ACTION_SEARCH you can handle a click on the keyboard search button. See this SO answer for more details.

    0 讨论(0)
  • 2020-12-05 10:49

    I had the same problem and end up with the following solution: custom SearchView + OnQueryTextListener.onQueryTextChange

    Custom SearchView:

    public class MySearchView extends SearchView {
    
    private boolean expanded;
    
    public MySearchView(Context context) {
        super(context);
    }
    
    @Override
    public void onActionViewExpanded() {
        super.onActionViewExpanded();
        expanded = true;
    }
    
    @Override
    public void onActionViewCollapsed() {
        super.onActionViewCollapsed();
        expanded = false;
    }
    
    public boolean isExpanded() {
        return expanded;
    }
    }
    

    Creating action and setting callback:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        searchAction = menu.add(0, SEARCH_ACTION_ID, 0 , getString(R.string.action_search));
        searchAction.setShowAsAction(SHOW_AS_ACTION_ALWAYS | SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);       
        searchView = new MySearchView(getSherlockActivity());
        searchView.setOnQueryTextListener(searchQueryListener);
        searchView.setIconifiedByDefault(true);
        searchAction.setActionView(searchView);
    }
    

    And last the listener:

    private OnQueryTextListener searchQueryListener = new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            search(query);
            return true;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            if (searchView.isExpanded() && TextUtils.isEmpty(newText)) {
                search("");
            }
    
            return true;
        }
    
        public void search(String query) {
            // reset loader, swap cursor, etc.
        }
    
    };
    

    Tested on ABS 4.3.

    0 讨论(0)
  • 2020-12-05 10:50

    I had the same problem, since empty query's are not supported I had to download and use ActionBarSherlock and then modify the onSubmitQuery() method.

    This is how my onSubmitQuery() looks like now

    private void onSubmitQuery() {
         CharSequence query = mQueryTextView.getText();
         if (query == null) {query = "";}
         if (mOnQueryChangeListener == null
                 || !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
             if (mSearchable != null) {
                 launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
                 setImeVisibility(false);
             }
             dismissSuggestions();
         }
     }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-05 10:50

    In my case I just wanted to enable user to clear his query as it was used as keyword in API search, so the simplest idea was:

        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userQuery = "";
            }
        });
    

    where userQuery is my variable used for searching etc.

    Hope it helps someone :)

    0 讨论(0)
  • 2020-12-05 10:57

    I can manage to do it simply by implementing the setOnQueryTextListener in this way:

    SearchView searchView = (SearchView) menu.findItem(R.id.searchProductView).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
    
            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    loadLocalData(newText);
                }
                return true;
            }
        });
    

    Where my loadLocalData method is

    //query my DB
    products = ProductDAO.findByIdList(idList);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    listView.setLayoutManager(layoutManager);
    listView.setAdapter(<YOURADAPTER>)
    

    This solution clears your query even if you clear the text with the "X" button

    0 讨论(0)
  • 2020-12-05 10:58

    ive an easier work around: use onQueryTextChange, but only render if its empty.

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                        renderList(true);
                        return false;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                        if (searchView.getQuery().length() == 0) {
                                renderList(true);
                        }
                        return false;
                }
        });
    
    0 讨论(0)
提交回复
热议问题