gpt4 book ai didi

android - setSuggestionAdapter() 不适用于 android.support.v7.widget.SearchView

转载 作者:行者123 更新时间:2023-11-30 00:38:33 25 4
gpt4 key购买 nike

我正在尝试通过以下方式在我的可搜索 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

我猜错误是由于我使用了 .setSuggestionAdapter() 不支持的 android.support.v7.widget.SearchView。所以我不得不改为使用我不想使用的 android.widget.SearchView

有人知道我该如何克服这个问题吗?是否有替代 .setSuggestionAdapter() 的方法?

感谢您的任何想法和提示!

最佳答案

支持 SearchView 类使用支持 CursorAdapter 类,而不是框架 CursorAdapter。只需确保您在 SuggestionCursorAdapter 类中导入了正确的那个。

import android.support.v4.widget.CursorAdapter;

关于android - setSuggestionAdapter() 不适用于 android.support.v7.widget.SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42964973/

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