gpt4 book ai didi

android - 正确显示 SearchView 建议

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:29 25 4
gpt4 key购买 nike

我最近在我的应用程序中添加了一个 SearchView(support-v7 库中提供的那个)。在我的例子中,提交按钮永远不应该使用 ACTION_SEARCH 启动一个新的 Intent,我只想显示一个建议列表,用户可以浏览并点击其中一个触发一些行动。

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

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

我应该在我的代码中更改什么才能使其正常工作?

代码:

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;
}
}

最佳答案

经过进一步测试,我决定不使用 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;
}

关于android - 正确显示 SearchView 建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26527134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com