Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionbarsherlock searchview: setOnQueryTextListener

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 onCreateOptionsMenu(final Menu menu) {
    getSupportMenuInflater().inflate(R.menu.building_search, menu);

    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
    {
        public boolean onQueryTextChange(String newText) 
        {
            // this is your adapter that will be filtered
            listAdapter.getFilter().filter(newText);
            return true;
        }

        public boolean onQueryTextSubmit(String query) 
        {
            // this is your adapter that will be filtered
            listAdapter.getFilter().filter(query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

These are my imports:

import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import be.ugent.zeus.hydra.AbstractSherlockActivity;
import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.data.caches.AssociationsCache;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSString;
import com.dd.plist.XMLPropertyListParser;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

Most of it works: in my actionbar, I get a search icon, which is clickable and expands the searchview. What, however, doesn't work is the actual listener. I've put breakpoints inside both methods, but when I debug, nothing happens. The program doesn't break and nothing gets filtered and I can't figure out why.

Does anyone has any idea?

Thanks in advance.

like image 626
Tom Naessens Avatar asked May 28 '26 23:05

Tom Naessens


1 Answers

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.

like image 162
Dave Agaba Avatar answered May 30 '26 15:05

Dave Agaba