Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchView - empty query doesn't work

I'm trying to add recent searches to the SearchView on a toolbar:

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

    @Override
    public boolean onQueryTextChange(String s) {
        if (s!= null )
        {
            if (s.isEmpty()){
                loadRecentList();
            }
            else{
                search(s);
            }
        }
        return true;
    }

});

The search is working fine, but I can't see the recent list results.

I found this Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string

but modifying the ActionBar library is not a nice solution.

Does anyone have another solution? Thanks.

Edit:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main_activity, menu);
    searchItem = menu.findItem(R.id.btn_earch);
    initSearchView();
    setSearchViewListener();
    return super.onCreateOptionsMenu(menu);
}



 protected void initSearchView() {
    final String CURSOR_RESOURCE_ID = "mCursorDrawableRes";
    final int THRESHOLD = 1;

    mSearchView = (SearchView) searchItem.getActionView();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSearchView.setMeasureWithLargestChildEnabled(true);

    autoCompleteTextView = (AutoCompleteTextView) mSearchView.findViewById(R.id.search_src_text);
    autoCompleteTextView.setThreshold(THRESHOLD);


}

protected void setSearchViewListener() {



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

        @Override
        public boolean onQueryTextChange(String s) {
            if (s!= null )
            {
                if (s.isEmpty()){
                    loadRecentList();
                }
                else{
                    search(s);
                }
            }
            return true;
        }

    });

btn_search item:

<item
    android:id="@+id/btn_search"
    android:icon="@drawable/ic_search_small"
    android:title="@string/tooltip_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"
    />
like image 751
Gali Issachar Avatar asked May 05 '15 07:05

Gali Issachar


1 Answers

I solve it with something like this

 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.i("MyApp", searchView.getQuery().toString());
            consultarListaParadas(searchView.getQuery().toString());
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    adaptadorListaParadasBus.setListaParadas(listaParadas);
                    adaptadorListaParadasBus.notifyDataSetChanged();
                }
            });

            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            if(newText.equals("")){
                this.onQueryTextSubmit("");
            }
            return true;
        }
    });

The only problem is that it throws without you press the search button.

like image 171
Hictus Avatar answered Sep 17 '22 16:09

Hictus