gpt4 book ai didi

android - 从不同 Activity 的不同表中搜索建议

转载 作者:IT王子 更新时间:2023-10-29 06:25:14 25 4
gpt4 key购买 nike

嗨,我已经关注了official android guideline用于启用搜索和搜索建议。我工作得很好,但问题是它只能从一个数据库表中搜索。

我有三个表和三个 Activity ,名称分别为 “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

    <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);
}



}
}

但是我如何通过使用此方法从相关表中获取数据来为每个 Activity 提供自定义搜索建议?

最佳答案

您只需添加另一个可搜索文件。您可以随意命名它,既然缺少任务,为什么不使用 searchable_tasks.xml

现在将所有现有的 searchable.xml 复制到此文件中,将 searchSuggestPath 更改为 TasksTable 并对 执行相同的操作searchSuggestIntentData.

当然,您还必须扩展您的内容提供者(将这些额外的可能性添加到 URIMatcher 并在查询方法中对相应的 URI 使用react)。

关于android - 从不同 Activity 的不同表中搜索建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19679569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com