Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 607
Maveňツ Avatar asked Dec 06 '22 05:12

Maveňツ


2 Answers

SearchView takes a CursorAdapter only:

  • setSuggestionsAdapter(CursorAdapter adapter)

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:

  • https://stackoverflow.com/a/11628527/1029225
like image 165
MH. Avatar answered Dec 23 '22 08:12

MH.


Please go through these links. You will get it what you want.

http://looksok.wordpress.com/2013/06/15/android-searchview-tutorial-edittext-with-phone-contacts-search-and-autosuggestion/

http://innovativenetsolutions.com/2013/07/android-tutorial-search-interface-search-dialog/

http://www.thaicreate.com/mobile/android-searchview.html

https://www.grokkingandroid.com/android-tutorial-adding-search-to-your-apps/

like image 28
Chintan Soni Avatar answered Dec 23 '22 07:12

Chintan Soni