Actionbarsherlock searchview: setOnQueryTextListener

后端 未结 4 1752
庸人自扰
庸人自扰 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 00:48

    In case you haven't found an answer,this is how I'm doing it (I am using ActionBarSherlock):

    • add an entry to the menu declaration

        
      
    • In your activity override"onCreateOptionsMenu" to inflate your menu and associate your SearcView :

      public boolean onCreateOptionsMenu(final Menu menu) {
          getSupportMenuInflater().inflate(R.menu.main_menu, menu);
          this.refreshMenuItem = menu.findItem(R.id.menu_refresh);
      
          this.searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
          this.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      
          @Override
          public boolean onQueryTextSubmit(String query) {
              // collapse the view ?
              menu.findItem(R.id.menu_search).collapseActionView();
              return false;
          }
      
          @Override
          public boolean onQueryTextChange(String newText) {
              // search goes here !!
              // listAdapter.getFilter().filter(query);
              return false;
          }
      
      });
      

    That's it, no other activities. Hope you find it useful.

提交回复
热议问题