SearchView小部件上未显示搜索下拉列表

时间:2016-02-04 14:55:57

标签: android android-toolbar searchview appbar

我在Android文档之后向SearchView widget添加了toolbar

问题是我没有获得dropdown之前的搜索。

我可以看到搜索建议正在通过SearchRecentSuggestionsProvider.insert(Uri uri, ContentValues values)添加到数据库中,但我没有看到任何调用SearchRecentSuggestionsProvider.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)返回先前结果(我假设应该调用它?)

下面的代码片段(我为此长时间的啰嗦道歉。)

我错过了什么?

我的gradle.build有:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId 'com.demonewsreader'
        minSdkVersion 19
        targetSdkVersion 23
 ....
dependencies {
 'com.android.support:appcompat-v7:23.1.1', with minSdkVersion 
...

我创建了SearchRecentSuggestionsProvider的子类:

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.demonewsreader.MySuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;

    public MySuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}

searchable.xml定义为:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/search_hint"
    android:label="@string/app_name"
    android:searchSuggestAuthority="com.demonewsreader.MySuggestionProvider"
    android:searchSuggestSelection=" ?"/>

我的AndroidManifest.xml通过以下方式定义搜索活动:

 <activity
        android:name=".ui.NewsArticleListActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </intent-filter>
    </activity>

我的搜索菜单项定义为:

<?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
    android:title="@string/search_title"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:searchSuggestAuthority="com.demonewsreader.MySuggestionProvider"
    android:searchSuggestSelection=" ?"/>

搜索视图在NewsArticleListActivity中定义:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    mMenu = menu;
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchView =  (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
    mSearchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    //mSearchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
    mSearchView.setSubmitButtonEnabled(true);

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.d(TAG," SearchView OnQueryTextListener.onQueryTextSubmit fired");
            mSearchString = mSearchView.getQuery().toString();
            saveSearchString(mSearchString);
            performRequest(mSearchString);
            mSearchView.onActionViewCollapsed();
            setProgressBarVisibility(View.VISIBLE);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            return true;
        }
    });
...
private void saveSearchString(String searchString) {
    SearchRecentSuggestions searchSuggestions = new SearchRecentSuggestions(this,
            MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
    searchSuggestions.saveRecentQuery(searchString, null);
}

包含SearchView的视图在布局中定义:

...
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/todays_headlines"
        android:textAppearance="?android:textAppearanceLarge"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>
...

0 个答案:

没有答案
相关问题