setSuggestionAdapter() 不适用于 android.support.v7.widget.SearchView
setSuggestionAdapter() not working with android.support.v7.widget.SearchView
我正在尝试通过以下方式在我的可搜索 activity 中设置自定义搜索建议适配器 (as explained in the documentation)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_result_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_result_search_widget).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
SuggestionCursorAdapter suggestionCursorAdapter = new SuggestionCursorAdapter(this, null);
searchView.setSuggestionsAdapter(suggestionCursorAdapter);
return true;
}
我的 SuggestionCursorAdapter.class 看起来像这样:
public class SuggestionCursorAdapter extends CursorAdapter {
public SuggestionCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text1 = (TextView) view.findViewById(R.id.list_item_text1);
TextView text2 = (TextView) view.findViewById(R.id.list_item_text2);
String text1String = cursor.getString(cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_1));
String text2String = cursor.getString(cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_2));
text1.setText(text1String);
text2.setText(text2String);
}
}
这会产生以下编译时间不兼容类型错误:
Error: incompatible types: SuggestionCursorAdapter cannot be converted to CursorAdapter
我想这个错误是我使用 android.support.v7.widget.SearchView
造成的,.setSuggestionAdapter()
不支持它。所以我不得不改为使用我不想使用的 android.widget.SearchView
。
有人知道我该如何克服这个问题吗? .setSuggestionAdapter()
有其他选择吗?
感谢任何想法和提示!
支持 SearchView
class 使用支持 CursorAdapter
class,而不是框架 CursorAdapter
。只需确保您在 SuggestionCursorAdapter
class.
中导入了正确的那个
import android.support.v4.widget.CursorAdapter;
我正在尝试通过以下方式在我的可搜索 activity 中设置自定义搜索建议适配器 (as explained in the documentation)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_result_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_result_search_widget).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
SuggestionCursorAdapter suggestionCursorAdapter = new SuggestionCursorAdapter(this, null);
searchView.setSuggestionsAdapter(suggestionCursorAdapter);
return true;
}
我的 SuggestionCursorAdapter.class 看起来像这样:
public class SuggestionCursorAdapter extends CursorAdapter {
public SuggestionCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text1 = (TextView) view.findViewById(R.id.list_item_text1);
TextView text2 = (TextView) view.findViewById(R.id.list_item_text2);
String text1String = cursor.getString(cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_1));
String text2String = cursor.getString(cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_2));
text1.setText(text1String);
text2.setText(text2String);
}
}
这会产生以下编译时间不兼容类型错误:
Error: incompatible types: SuggestionCursorAdapter cannot be converted to CursorAdapter
我想这个错误是我使用 android.support.v7.widget.SearchView
造成的,.setSuggestionAdapter()
不支持它。所以我不得不改为使用我不想使用的 android.widget.SearchView
。
有人知道我该如何克服这个问题吗? .setSuggestionAdapter()
有其他选择吗?
感谢任何想法和提示!
支持 SearchView
class 使用支持 CursorAdapter
class,而不是框架 CursorAdapter
。只需确保您在 SuggestionCursorAdapter
class.
import android.support.v4.widget.CursorAdapter;