2

I have a working custom search suggestions class (via http://developer.android.com/guide/topics/search/adding-custom-suggestions.html). It currently returns one type of information - "product names".

I've added some additional activities (screens) to my app so that if a person is on a product page, starting up a search should return results from "product names", but if they are in another activity, I would like the search suggestions to pull "manufacturer names".

I saw Accessing appSearchData bundle from ContentProvider, but the custom search suggestions provider extends ContentProvider which doesn't work with the answer

Bundle b = intent.getBundleExtra(SearchManager.APP_DATA);

This Bundle is available to the search results class, but not the contentprovider.

How best to pass a parameter ("product" or "manufacturer") to a search suggestions content provider?

Community
  • 1
  • 1
XtopherSD
  • 333
  • 2
  • 11

2 Answers2

3

This doesn't seem like an ideal solution, but I had the same need and I found I could get the job done by adding a public ivar or method to the subclass of ContentProvider that handles the search suggestions. Prior to initiating the search, you can configure your provider as needed. You can access the provider instance from an activity like so:

ContentProviderClient client = getContentResolver().acquireContentProviderClient("my.searchsuggestionprovider");
MyProviderClass provider = (MyProviderClass) client.getLocalContentProvider();

Now you can configure with provider.setParameter("product") or whatever you need. You might need to reset the parameter to a default or something after building your cursor.

Edit: This turned out to be impractical, at least in my case, since the content provider is called with query() every time a character is typed. Instead, I have employed a workaround similar to what is described in set-search-hint-dynamically. By creating an alternate "searchable" XML definition and activity, you can alter the URI that's passed to the content provider in query(), adding a path component to provide the additional parameter or context you need.

Community
  • 1
  • 1
bugeye
  • 181
  • 1
  • 4
0

I just made a static variable for the parameter on my content provider, and set it from the activity. I been thinking about it, and it's the cleanest workaround I have found!

Carlos Ginés
  • 1,192
  • 1
  • 7
  • 9