Android, multiple types of search?
How can I do two different search operations?
I have different search logic in different parts of my application (what I am looking for and how I display the results).
For example, in some activities I want to query my online database for class A objects, and in others I want to query my online database for class B objects.
I currently have a setup to search from the following guide: http://developer.android.com/guide/topics/search/search-dialog.html and is great for finding class A objects. However, I couldn't figure out how do the second search operation, which has different layout and query logic. Seems like you can apply searchable metadata to a single action?
There must be a way to do this, right?
source to share
Yes, there is a way to do it.
If you only want to have one default search operation in your application and other numerous actions that send a search query to it, you can provide the application search metadata when you create a search type in your specific activity, as shown below:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
Bundle bundle = new Bundle();
bundle.putString("Search Type", "local search");
bundle.putString("Other metadata", "metadata");
searchView.setAppSearchData(bundle);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
}
Then get the metadata in the default search app like below:
mSearchQuery = intent.getStringExtra(SearchManager.QUERY);
Bundle bundle = intent.getBundleExtra(SearchManager.APP_DATA);
// Get the type of search to perform
mSearchType = bundle.getString("Search Type");
source to share