4

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);
}
Maveňツ
  • 9,147
  • 14
  • 53
  • 94
  • See this link,It may help you, http://stackoverflow.com/questions/15805397/android-searchview-with-auto-complete-feature-inside-action-bar – shiju B Oct 12 '13 at 05:34
  • pls don't suggest me to use custom layout and setting it to action bar with defining xml file :) – Maveňツ Oct 12 '13 at 05:35
  • 2
    see this link http://looksok.wordpress.com/2013/06/15/android-searchview-tutorial-edittext-with-phone-contacts-search-and-autosuggestion/ – pskink Oct 12 '13 at 05:56

2 Answers2

11

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:

Community
  • 1
  • 1
MH.
  • 44,788
  • 10
  • 100
  • 115