Sha*_*zal 6 sqlite android android-contentprovider searchview
嗨,我已经遵循官方的Android指南启用搜索和搜索建议.我工作得很好,但问题是它只从一个数据库表中搜索.
我有三个表和一个名为三项活动“BirthdayWisher_Table”,“Tasks_Table”,“Events_Table”和“BirthdayWisher_Activity”,“Tasks_Activity”,“Events_Activity”分别.
我希望当用户处于"BirthdayWisher_Activity"并按下搜索菜单项时,我的应用程序应搜索"BirthdayWisher_Table"中的数据,当用户处于"Tasks_Activity"时,按下搜索菜单项,我的应用程序应搜索来自"Tasks_Table".
但对我来说似乎不可能这样做.
<?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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
android:searchSuggestIntentData="content://com.beaconimpex.assistant.app.ContentProvider/BirthdayWishTable"
android:searchSuggestPath="BirthdayWishTable"
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如何通过使用此方法从相关表中获取数据,为每个活动提供自定义搜索建议?
您只需添加另一个可搜索的文件.你可以随心所欲地命名,所以既然缺少任务,为什么不使用searchable_tasks.xml呢?
现在将所有现有内容复制searchable.xml到此文件中,更改searchSuggestPath为TasksTable并执行相同操作searchSuggestIntentData.
当然,您还必须扩展内容提供程序(将这些额外的可能性添加到URIMatcher并对查询方法中的相应URI做出反应).
您应该对所有表 \xe2\x80\x9cBirthdayWisher_Table\xe2\x80\x9d、\xe2\x80\x9cTasks_Table\xe2\x80\x9d、\xe2\x80\x9cEvents_Table\xe2\x80\x9d 和 \ 使用数据库联接xe2\x80\x9cBirthdayWisher_Activity\xe2\x80\x9d、\xe2\x80\x9cTasks_Activity\xe2\x80\x9d、\xe2\x80\x9cEvents_Activity\xe2\x80\x9d 分别检索所有表信息。希望这个链接可以帮助您理解我在说什么。然后就可以解析搜索建议中的所有表信息
\n\n使用 AutoCompleteTextview 的另一种方法
\n\n看看这个源代码
\n\nSuggestionAdapter 类将参数“type”放入构造函数中。
\n\n String type;\n public SuggestionAdapter(Activity context, String nameFilter,String type) {\n super(context, android.R.layout.simple_dropdown_item_1line);\n suggestions = new ArrayList<String>();\n this.type=type;\n }\nRun Code Online (Sandbox Code Playgroud)\n\n然后通过 type\non getFilter() 方法检索信息
\n\nList<SuggestGetSet> new_suggestions = db.getInformation(type)\nRun Code Online (Sandbox Code Playgroud)\n\n然后使用 AutoCompleteTextView 如下
\n\n AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.autoComplete);\n\n acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString(),"Tasks_Table"));\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2033 次 |
| 最近记录: |