Search code examples
androidsearchview

How to set adapter to searchview in actionbar


Setting custom values to Searchview Adapter

like we do in autocomplete and passing array string

I tried this code:

private void setupSearchView(MenuItem searchItem) {

    if (isAlwaysExpanded()) {
        mSearchView.setIconifiedByDefault(false);
    } else {
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if (searchManager != null) {
        List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
        for (SearchableInfo inf : searchables) {
            if (inf.getSuggestAuthority() != null && inf.getSuggestAuthority().startsWith("applications")) {
                info = inf;
            }
        }
        mSearchView.setSearchableInfo(info);

    }
    mSearchView.setOnQueryTextListener(this);
}

Solution

  • SearchView takes a CursorAdapter only:

    Unfortunately that means you can't just supply an ArrayAdapter with an array of items. If you really wanted to use a String[] as searchable data source, I suppose you could wrap it into a MatrixCursor.

    An example is can be found here: