Actionbarsherlock searchview: setOnQueryTextListener

后端 未结 4 1630
庸人自扰
庸人自扰 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

        <item android:id="@+id/menu_search"
          android:actionViewClass="com.actionbarsherlock.widget.SearchView"
          android:icon="@drawable/wfm_menu_search"
          android:showAsAction="always|withText|collapseActionView"
          android:title="@string/menu_search"/>
      
    • 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.

    0 讨论(0)
  • 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:

        <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/ic_search"
          android:showAsAction="collapseActionView|ifRoom" 
          android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
    

    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:

        <activity android:name=".SearchActivity"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
            <meta-data 
                android:name="android.app.searchable"
                android:resource="@xml/searchable"/>
        </activity>
    

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

        <meta-data android:name="android.app.default_searchable"
                        android:value=".SearchActivity" />
    

    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.

    0 讨论(0)
  • 2021-01-07 01:12

    As every other answer contained a bit of truth, but didn't answer the question fully, I'll create my own answer.

    When this issue was created - half a year ago - ABS did not fully implement the SearchView functionality. As of ABS 4.2.0, this has been implemented.

    My full implementation, by following the ABS sample app, can be found here.

    0 讨论(0)
  • 2021-01-07 01:14

    Is that your full code? Right now in the onCreateOptionsMenu you create a SearchView and assign to it a OnQueryTextListener but that is all you do with it. I don't see in your code where you add this SearchView to the inflated menu.

    As you say you see the widget on the screen, I'm assuming you have declared the widget in the R.menu.building_search file, in which case you should look for that widget(which doesn't have a listener set on it so no action is performed) and not declare a new one(which will not interact with the user):

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    

    (This is from the Search guide on the android developer site, if I'm not mistaken this should work for ActionBarSherlock as well).

    0 讨论(0)
提交回复
热议问题