正确显示SearchView建议

时间:2014-10-23 11:33:50

标签: android android-support-library android-search

我最近在我的应用程序中添加了SearchViewsupport-v7库中提供的那个)。在我的情况下,提交按钮永远不应该使用Intent开始新的ACTION_SEARCH,我只想显示用户可以浏览的建议列表,然后点击其中一个会触发某些操作。

我认为一切都已正确设置,但我有两大问题:

  • 第一次输入内容时,即使触发了所有事件(我使用了一些日志打印来检查),建议列表也没有显示,我必须清除搜索文本并重新开始写入,然后是建议显示。这只是在我第一次搜索某些内容时才会发生。如果我关闭并重新打开SearchView,则会显示自首次尝试后的建议。
  • 要加载建议,我通过ContentProvider查询LoaderManager,如果输入太快,应用程序崩溃说我正在尝试重新打开已关闭的对象(我猜通过查询ContentProvider得到的光标。

我应该在代码中更改哪些内容才能使其正常工作?

代码:

onCreateOptionsMenu中的

mActivity.getMenuInflater().inflate(R.menu.itemselect_search, menu);
SearchManager searchManager = (SearchManager) mActivity
        .getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.item_search)
        .getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity
        .getComponentName()));
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);

我的onQueryTextListener

public boolean onQueryTextSubmit(String query) {
    return true;
}
public boolean onQueryTextChange(String newText) {
    Log.i("TextChange", "=(" + newText + ")");
    if (newText == null || newText.isEmpty()) {
        //Empty the suggestions instead of showing all items...
        if (null != mSuggestionAdapter)
            mSuggestionAdapter.swapCursor(null);
    } else {
        currentSearchQuery = newText;
        mActivity.getSupportLoaderManager().restartLoader(1, null, this);
    }
    return true;
}

我的onCreateLoader

public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
    CursorLoader mCursorLoader = null;
    switch (id) {
    //Other Loader IDs...
    case 1:
        Log.i("OnCreateLoader", "Loader created: " + id);
        mCursorLoader = new CursorLoader(mActivity,
                MyContentProvider.URI,
                SQLiteRomDataSource.allColumns,
                mSelection,
                mSelectionArgs, null);
        break;
    }
    return mCursorLoader;
}

我的onLoadFinished

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    switch (loader.getId()) {
    //Other Loader IDs...
    case 1:
        Log.i("OnLoadFinished",
                "Loader " + loader.getId() + ", " + cursor.getCount()
                        + " results");
        if (null == mSuggestionAdapter) {
            Log.i("OnLoadFinished","Creating adapter");
            mSuggestionAdapter = new SuggestionsCursorAdapter(
                    mActivity, cursor, 0);
        }
        if (searchView.getSuggestionsAdapter() != mSuggestionAdapter){
            Log.i("OnLoadFinished","Binding adapter to searchview");
            searchView.setSuggestionsAdapter(mSuggestionAdapter);
        }
        mSuggestionAdapter.swapCursor(cursor);
        Log.i("OnLoadFinished","Swapping cursor...");
        break;
    }
}

最后我的onLoaderReset

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    switch (loader.getId()) {
    //Other Loader IDs...
    case 1:
        mSuggestionAdapter.swapCursor(null);
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

经过进一步测试后,我决定不使用LoaderManager并在onQueryTextChange方法中执行所有操作。这两个问题似乎都得到了解决。

public boolean onQueryTextChange(String newText) {
    Log.i("TextChange", "=(" + newText + ")");
    if (newText == null || newText.isEmpty()) {
        if (null != mSuggestionAdapter)
            mSuggestionAdapter.swapCursor(null);
    } else {
        Cursor mCur = mActivity.getContentResolver().query(
                MyContentProvider.URI,
                DataSource.allColumns,
                mSelection, mSelectionArgs, null);
        if(null == mRomSuggestionAdapter){
            mRomSuggestionAdapter = new SuggestionsCursorAdapter(mActivity,mCur,0);
            searchView.setSuggestionsAdapter(mRomSuggestionAdapter);
        }
        if (null != mSuggestionAdapter)
            mSuggestionAdapter.swapCursor(mCur);
    }
    return true;
}
相关问题