在不同的活动中搜索不同表格的建议

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".

但对我来说似乎不可能这样做.

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>
Run Code Online (Sandbox Code Playgroud)

这是我如何关联我的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>
Run Code Online (Sandbox Code Playgroud)

它对BirthdayWisherTable非常有效(因为我已指定)

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)

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

Wol*_*yer 7

您只需添加另一个可搜索的文件.你可以随心所欲地命名,所以既然缺少任务,为什么不使用searchable_tasks.xml呢?

现在将所有现有内容复制searchable.xml到此文件中,更改searchSuggestPathTasksTable并执行相同操作searchSuggestIntentData.

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

  • 提及`searchSuggestPath`的+1 (2认同)

Mun*_*oor 2

您应该对所有表 \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\n

SuggestionAdapter 类将参数“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    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后通过 type\non getFilter() 方法检索信息

\n\n
List<SuggestGetSet> new_suggestions = db.getInformation(type)\n
Run 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"));\n
Run Code Online (Sandbox Code Playgroud)\n