【问题标题】:Search suggestions from different tables in different activities从不同活动的不同表格中搜索建议
【发布时间】:2013-10-30 10:21:15
【问题描述】:

您好,我已关注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);
    }



       }
}

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

【问题讨论】:

    标签: android sqlite android-contentprovider searchview


    【解决方案1】:

    您只需添加另一个可搜索的文件。你可以随意命名,既然任务不见了,为什么不使用searchable_tasks.xml呢?

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

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

    【讨论】:

      【解决方案2】:

      您应该对所有表“BirthdayWisher_Table”、“Tasks_Table”、“Events_Table”和“BirthdayWisher_Activity”、“Tasks_Activity”、“Events_Activity”分别使用数据库连接来检索所有表信息。希望Link 可以帮助您理解我在说什么。那么你就可以解析搜索建议中的所有表格信息

      AutoCompleteTextview 的另一种使用方式

      看看这个source code

      SuggestionAdapter 类将参数“type”放入构造函数中。

         String type;
         public SuggestionAdapter(Activity context, String nameFilter,String type) {
                  super(context, android.R.layout.simple_dropdown_item_1line);
                  suggestions = new ArrayList<String>();
                  this.type=type;
          }
      

      然后按类型检索信息 关于 getFilter() 方法

      List<SuggestGetSet> new_suggestions = db.getInformation(type)
      

      然后像下面这样使用 AutoCompleteTextView

        AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.autoComplete);
      
        acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString(),"Tasks_Table"));
      

      【讨论】:

      • 使用此方法我们如何在 Content Provider 的 query 方法中进行识别,以便它检查是否应返回 BirthdayWisher_TableTask_TableEvents_Table ....
      • 但我在 ActionBar 中使用 android.widget.SearchView,正如问题中所述,我只想知道是否有可能让每个 Activity 的 SearchView 菜单显示来自不同数据库表的 SearchSuggestions。但是,如果 somone 会给我一个很好的参考,这是迄今为止不可能的,那么我会寻找自定义解决方案。
      【解决方案3】:

      如果我没记错的话,您可以通过以下方式实现:

      传递活动名称以搜索活动。 从意图中获取调用的活动名称。 现在你有了被调用的活动名称,你可以为所欲为。

      【讨论】:

      • 其实这个问题是关于来自多个表的搜索建议。在搜索建议的情况下,searchView 会自动调用我的contentProviderquery 方法,这意味着我们不会为此使用意图。
      猜你喜欢
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      相关资源
      最近更新 更多