Actionbarsherlock searchview: setOnQueryTextListener

后端 未结 4 1629
庸人自扰
庸人自扰 2021-01-07 00:12

I\'m trying to make a filter in a List using ActionBarSherlock\'s search view. The code I currently have is the following:

@Override
public boolean onCreateO         


        
4条回答
  •  孤街浪徒
    2021-01-07 01:04

    This is how I implemented my search with ActionBarSherlock:

    In the menu.xml under the res/menu folder, I added an icon:

        
    

    I then created a class which is responsible for recieving the action search queries and presenting the data:

        public class SearchActivity extends SherlockFragmentActivity{
    
        @Override
            protected void onCreate(Bundle savedInstanceState) {
           Log.d(TAG, "This is the search view activity");
           // TODO Auto-generated method stub
           super.onCreate(savedInstanceState);
           setContentView(R.layout.search_result_layout);
                }
    
    
        private void handleIntent(Intent intent){
            if(Intent.ACTION_SEARCH.equals(intent.getAction())){
            searcdhQuery = intent.getStringExtra(SearchManager.QUERY);
            //here we shall do e search..
            Log.d(TAG, "This is the search query:" + searcdhQuery);
    
                        //This is the asynctask query to connect to the database...
            String[] value = {searcdhQuery};
            SearchQuery searchQuery = new SearchQuery();
            searchQuery.execute(value);
            }
            }
    
                @Override
            protected void onNewIntent(Intent intent) {
             // TODO Auto-generated method stub
             super.onNewIntent(intent);
             handleIntent(intent);
            }
            }
    

    In the manifest, this activity is included with Search and View filters as Shown below:

        
            
                
            
            
                
            
            
        
    

    Also in the manifest, don't forget the meta-data in the application showing the default search of the activity, as shown below:

        
    

    Finally in the onCreateOptionsMenu, be sure to add the search configuration by associating it with the search service:

        //associating the searchable configuration with the search service...
        SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView)menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    

    Nothing is required in the onOptionsItemSelected method.

    This worked perfectly for me. In case you need more details please find more information from the Search View Demo in the actionBarSherlock and The developers tutorial on the Setting a Search Interface.

    I hope this helps.

提交回复
热议问题