How to set adapter to searchview in actionbar

巧了我就是萌 提交于 2019-12-06 04:16:05

问题


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);
}

回答1:


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



回答2:


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/



来源:https://stackoverflow.com/questions/19330912/how-to-set-adapter-to-searchview-in-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!