Android: Return search query to current activity

前端 未结 9 1149
半阙折子戏
半阙折子戏 2020-12-07 16:34

I followed the steps described on http://developer.android.com/guide/topics/search/search-dialog.html to implement a search feature in my notepad application.

My pro

9条回答
  •  被撕碎了的回忆
    2020-12-07 17:12

    I was also facing the same problem, then I wrote this code and it solved my problem.

    Implement this in your Activity

    **implements SearchView.OnQueryTextListener, SearchView.OnCloseListener**
    

    Add this function in the class:

    private void setupSearchView()
            {
    
                searchView.setIconifiedByDefault(true);
    
                SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                if (searchManager != null)
                    {
                        List searchables = searchManager.getSearchablesInGlobalSearch();
    
                        // Try to use the "applications" global search provider
                        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
                        for (SearchableInfo inf : searchables)
                            {
                                if (inf.getSuggestAuthority() != null && inf.getSuggestAuthority().startsWith("applications"))
                                    {
                                        info = inf;
                                    }
                            }
                        searchView.setSearchableInfo(info);
                    }
    
                searchView.setOnQueryTextListener(this);
                searchView.setOnCloseListener(this);
            }
    

    Call the function in the onCreateOptionsMenu(Menu menu)

    @Override
        public boolean onCreateOptionsMenu(Menu menu)
            {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.menu, menu);
                //restoreActionBar();
                // Associate searchable configuration with the SearchView
                //SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
                //searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
                setupSearchView();
                return super.onCreateOptionsMenu(menu);
            }
    

    This will totally solve your problem..

    Happy Coding!!

提交回复
热议问题