微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 从不同活动中的不同表中搜索建议

嗨我已经按照 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);
    }



       }
}

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

解决方法

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

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

当然,您还必须扩展内容提供程序(将这些额外的可能性添加到URIMatcher并对查询方法中的相应URI做出反应).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐