【问题标题】:Custom Search Suggestion not showing up in Android Application自定义搜索建议未显示在 Android 应用程序中
【发布时间】:2015-09-13 09:04:43
【问题描述】:

我尝试了 Google 提供的以下指南:Adding Custom Suggestion。我的应用程序的搜索功能正常工作,但无法显示搜索建议。

如果我在 SearchView 处于活动状态时键入内容,它根本不会显示任何建议。例如,我得到了 3 个项目,其中包含世界 Hello。当我输入 hello 时,我希望 SearchView 下方会出现某种列表视图。没有。是因为我的数据库还没有太多内容吗?

这就是我到目前为止所得到的。如果你能指出我错过了什么,那就太好了!

Android 清单文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.neonwarge.android.note"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="23" />

    <application
        android:name=".NoteApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".activities.HomeScreenActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>            

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

        </activity>

        <activity
            android:name="com.neonwarge.android.note.activities.NoteActivity"
            android:label="@string/activity_label_note"
            android:launchMode="singleTop" >

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.neonwarge.android.note.activities.HomeScreenActivity" />

        </activity>

        <activity
            android:name="com.neonwarge.android.note.activities.NoteSettingsActivity"
            android:label="@string/activity_label_notesettings" >

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.neonwarge.android.note.activities.NoteActivity" />

        </activity>

        <activity
            android:name="com.neonwarge.android.note.activities.SearchResultActivity"
            android:label="@string/title_activity_search_result"
            android:launchMode="singleTop">

            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.neonwarge.android.note.activities.HomeScreenActivity"/>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

            <meta-data
                android:name="android.app.default_searchable"
                android:value="com.neonwarge.android.note.activities.SearchResultActivity"/>

        </activity>

        <receiver android:name=".receivers.NoteAlarmReceiver" />

        <provider
            android:name="com.neonwarge.android.note.providers.NoteProvider"
            android:authorities="com.neonwarge.android.note.providers.NoteProvider"
            android:exported="false"
            android:multiprocess="true" />       

    </application>

</manifest>

内容提供者:

    mUriMatcher.addURI(AUTHORITY , SearchManager.SUGGEST_URI_PATH_QUERY , NOTE_SEARCH_SUGGESTIONS);

...

@Override
public Cursor query(Uri uri, String[] projection, String whereClause, String[] selection,String sortorder) 
{
    (...)   
    case NOTE_SEARCH_SUGGESTIONS:

            Log.i(NoteApplication.TAG , "NoteProvider.query() : search suggest enabled...");
            Log.i(NoteApplication.TAG , "URI : " + uri.toString());
            Log.i(NoteApplication.TAG , "Where " + whereClause);
            Log.i(NoteApplication.TAG , "Searching for : " + selection[0]);

            projection = 
                new String[]
                {
                      "rowid as " + NoteDatabaseHelper.NoteEntry._ID
                    , NoteDatabaseHelper.NoteEntry.COLUMN_SHORT_NOTE + " as " + SearchManager.SUGGEST_COLUMN_TEXT_1
                    , NoteDatabaseHelper.NoteEntry.COLUMN_LONG_NOTE + " as " + SearchManager.SUGGEST_COLUMN_TEXT_2
                };

            cursor = db.query(NoteDatabaseHelper.NoteEntry.VIRTUAL_TABLE_NAME , projection , whereClause , selection, null, null, null);
            break;

        default:
            throw new IllegalArgumentException("Unknown uri " + uri);
    }

    return cursor;
}

...
@Override
public String getType(Uri uri) 
{
    Log.i(NoteApplication.TAG , "NoteProvider.getType() : called.");
    switch(mUriMatcher.match(uri))
    {
        case NOTE_SEARCH_SUGGESTIONS:
            Log.i(NoteApplication.TAG , "NoteProvider.getType(): type: NOTE_SEARCH_SUGGESTIONS");
            return SearchManager.SUGGEST_MIME_TYPE;

        default:
            throw new IllegalArgumentException("NoteProvider.getType(): Unknown URL : " + uri);
    }
}
...

可搜索的.xml:

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"    
    android:searchSuggestAuthority="com.neonwarge.android.note.providers.NoteProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:searchSuggestSelection="ShortNote MATCH ? and LongNote MATCH ?">
</searchable>

我从 Activity 的片段中扩充了选项菜单布局:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu,  inflater);
    inflater.inflate(mIsOnSearchMode? R.menu.fragment_search_result : R.menu.fragment_note_list, menu);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getActivity().getPackageName() , SearchResultActivity.class.getName())));

    searchView.setSuggestionsAdapter(new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_list_item_1,
            null,
            new String[]{SearchManager.SUGGEST_COLUMN_TEXT_1 , SearchManager.SUGGEST_COLUMN_TEXT_2},
            new int[]{android.R.layout.simple_list_item_1},
            0));

    searchView.setIconifiedByDefault(true);
}

我已在我的工作区中加载了 SearchableDictionary 示例,我相信我已将每个位置都考虑在内。也许我在这里期待一些错误?显示建议的列表是否需要适配器?因为我提供了一个,它仍然没有显示。或者,在建议生效之前,我可能需要一百万个内容。另外,我只是重用了现有的内容提供程序,是否必须使用其他提供程序?

请告诉我我错过了什么。

【问题讨论】:

  • 我在链接中发布的 15 行代码?你看到我的链接了吗?
  • 我也在使用自定义的 SimpleCursorAdapter 那么有什么关系?我的 15 行代码运行了吗?
  • 那么告诉我为什么我发布了那个代码?不工作的代码?那是什么原因呢?
  • 什么不起作用?有什么理由吗?我在您的回答中看不到任何内容,为什么您不能在 runQueryOnBackgroundThread 方法中返回您的数据
  • 我不知道你在说什么:如果你从 ContentProvider#query 或 CursorAdapter#runQueryOnBackgroundThread 返回游标绝对没有区别,区别在于你的代码有 100 行代码: (自定义 ContentProvider、searchable.xml、清单中的更改等)虽然我有 15 个,但选择是你的

标签: java android android-layout


【解决方案1】:

这是一个非常非常小的问题。我输入了查询虚拟表的“错误”sql 语法!

我最初是这样输入的:"ShortNote MATCH ? and LongNote MATCH ?" 这是错误的,因为我的表格没有命名为 ShortNote 和 LongNote。我应该改用这种语法:

NAME_OF_FTS3_TABLE match ?

我误解了android:searchSuggestSelection,忘记了我使用 FTS3 表来查找一些数据,错误地像普通表一样查询它。

我所做的只是为 android:searchSuggestSelection 提供了一个非空值,并在 ContentProvider 中自己处理 whereClause,相关建议列表现在显示在搜索小部件/搜索对话框下方。

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多