【发布时间】:2017-03-23 00:13:37
【问题描述】:
我正在尝试通过以下方式在我的可搜索活动中设置自定义搜索建议适配器 (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);
}
}
这会产生以下编译时间不兼容类型错误:
错误:不兼容的类型:SuggestionCursorAdapter 无法转换为 CursorAdapter
我猜这个错误来自我使用android.support.v7.widget.SearchView,.setSuggestionAdapter() 不支持。所以我不得不改用我不想使用的android.widget.SearchView。
有人知道我该如何克服这个问题吗? .setSuggestionAdapter() 有什么替代品吗?
感谢您的任何想法和提示!
【问题讨论】:
-
听起来你输入了错误的
CursorAdapter类。确保您使用的是the supportCursorAdapter-import android.support.v4.widget.CursorAdapter;。 -
谢谢,解决了这个问题,你想把它作为这个问题的答案吗?
标签: android searchview