Skip to content

Instantly share code, notes, and snippets.

@StefMa
Created July 24, 2015 14:26
Show Gist options
  • Save StefMa/86964e3a11ce52662be9 to your computer and use it in GitHub Desktop.
Save StefMa/86964e3a11ce52662be9 to your computer and use it in GitHub Desktop.
SearchView suggestions over network

SearchView suggestions over network


What is it?

This is a simple how to make a network request with the SearchView implemented in a Toolbar.


Why?

The documentation for a Search Interface says that you can't make a a request over the network. You can only make a SearchView via recent searches or over a ContentProvider. I want to demonstrate how to make network request with the searchview.


How to use?

First thing is to setup the SearchView despripted here: Creating a Search Interface

Your onCreateOptionsMenu(Menu menu) should look like this:

SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
            (SearchView) menu.findItem(R.id.action_search).getActionView();
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

To setup suggestions you must put an adapter to the SearchView:

final CursorAdapter suggestionAdapter = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_list_item_1,
            null,
            new String[]{SearchManager.SUGGEST_COLUMN_TEXT_1},
            new int[]{android.R.id.text1},
            0);
    searchView.setSuggestionsAdapter(suggestionAdapter);

For suggestions based on the input you must set a onQueryTextListener.

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

        @Override
        public boolean onQueryTextChange(String newText) {
            // Put your magic here
            return false;
        }
    });

Now it's time for magic. On your onQueryTextChange(String newText) you can call your AsyncTask* and set the result (from the network) to your adapter (which was set to your SearchView).

new Task(new Task.Listener() {
                @Override
                public void onReady(List<String> results) {
                    String[] columns = {
                            BaseColumns._ID,
                            SearchManager.SUGGEST_COLUMN_TEXT_1,
                            SearchManager.SUGGEST_COLUMN_INTENT_DATA
                    };

                    MatrixCursor cursor = new MatrixCursor(columns);

                    for (int i = 0; i < results.size(); i++) {
                        String[] tmp = {Integer.toString(i),  results.get(i), "COLUMNT_INTENT_DATA"};
                        cursor.addRow(tmp);
                    }

                    suggestionAdapter.changeCursor(cursor);
                }
            }).execute();

*To make your network request you need an AsyncTask (which is described here: AsyncTask)

@tomerpetel
Copy link

Sooooo helpful!!! Many thanks @StefMa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment