嗨我已经按照
official android guideline启用了搜索和搜索建议.我工作得很好,但问题是它只从一个数据库表中搜索.
我有三个表和三个活动,分别名为“BirthdayWisher_Table”,“Tasks_Table”,“Events_Table”和“BirthdayWisher_Activity”,“Tasks_Activity”,“Events_Activity”.
我希望当用户处于“BirthdayWisher_Activity”并按下搜索菜单项时,我的应用程序应搜索“BirthdayWisher_Table”中的数据,当用户处于“Tasks_Activity”时,按下搜索菜单项,我的应用程序应搜索来自“Tasks_Table”.
但对我来说似乎不可能这样做.
SearchAble配置文件
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search" android:label="@string/app_name" android:searchSettingsDescription="@string/search_the_entire_app_" android:searchSuggestAuthority="com.beaconimpex.assistant.app.ContentProvider" android:searchSuggestIntentAction="android.Intent.action.VIEW" android:searchSuggestIntentData="content://com.beaconimpex.assistant.app.ContentProvider/BirthdayWishTable" android:searchSuggestPath="BirthdayWishTable" android:searchSuggestSelection=" suggest_text_1 LIKE ? " android:searchSuggestThreshold="1" > </searchable>
这是我如何关联我的searchAble活动
<activity android:name="com.beaconimpex.assistant.SearchAppActivity" android:label="@string/title_activity_search_app" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <Meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity>
它对BirthdayWisherTable非常有效(因为我已指定)
android:searchSuggestIntentData="content://com.beaconimpex.assistant.app.ContentProvider/BirthdayWishTable" android:searchSuggestPath="BirthdayWishTable"
我的内容提供商
public class AssistantContentProvider extends ContentProvider { public static final String DATABASE_NAME = "gcuf__dB"; public static final int DATABASE_VERSION = 99; public static final String AUTHORITY = "com.beaconimpex.assistant.app.ContentProvider"; sqliteDatabase db; private DBOpenHelper dbHelper; private static final UriMatcher sURIMatcher = new UriMatcher( UriMatcher.NO_MATCH); private static final int SEARCH_SUGGEST = 12345; static { sURIMatcher.addURI(AUTHORITY,BirthdayWisher.TABLE_NAME + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,SEARCH_SUGGEST); sURIMatcher.addURI(AUTHORITY,BirthdayWisher.TABLE_NAME + "/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*",SEARCH_SUGGEST); } @Override public Cursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder) { Log.i(MyConstants.LOG_TAG,"-----Query method is called with URI: " + uri.toString()); switch (sURIMatcher.match(uri)) { case SEARCH_SUGGEST: Log.i(MyConstants.LOG_TAG,"URI Search Suggest"); if (selectionArgs != null && selectionArgs.length > 0 && selectionArgs[0].length() > 0) { // the entered text can be found in selectionArgs[0] // return a cursor with appropriate data return queryCursor(uri,BirthdayWisher.TABLE_NAME,BirthdayWisher.allColumns,selection,new String[] { "%" + selectionArgs[0].toLowerCase() + "%" },null); } else { // user hasn't entered anything // thus return a default cursor Log.i(MyConstants.LOG_TAG,"User has not entered anything in the searchBox."); return null; } default: throw new IllegalArgumentException("Unsupported URI: " + uri); } } }