自定义搜索建议未显示在Android应用程序中

时间:2015-09-13 09:04:44

标签: java android android-layout

我尝试了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);
    }
}
...

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

我从活动的片段中夸大了选项菜单布局:

@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示例,我相信我得到的每个点都有。也许我在想这里有什么问题?显示建议的列表是否需要适配器?因为我提供了一个,它仍然没有显示。或者在建议工作之前我可能需要一百万个内容。另外,我只是重用了现有的内容提供商,我是否必须使用其他提供商?

请让我知道我错过了什么。

1 个答案:

答案 0 :(得分:0)

这是一个非常小的陷阱。我输入'WRONG'sql语法来查询虚拟表!

我最初输入的方式如下:"ShortNote MATCH ? and LongNote MATCH ?"这是错的,因为我的表没有命名为ShortNote和LongNote。我应该使用这种语法:

NAME_OF_FTS3_TABLE match ?

我误解了android:searchSuggestSelection,忘记了我正在使用FTS3表寻找一些数据,错误地查询它就像是一张普通的表。

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

谢谢!

相关问题