我最近在我的应用程序中添加了SearchView
(support-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;
}
}
答案 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;
}