android - SearchView:从建议监听器中获取选中的项目

标签 android android-arrayadapter simplecursoradapter searchview matrixcursor

我有一个搜索 View ,其中包含由 MatrixCursor 填充的建议(因为我已经有一个字符串数组)。但是我想得到用户正在选择哪个项目。到目前为止,我只能获得用户点击建议列表的位置:

searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
        @Override
        public boolean onSuggestionClick(int position) {
            String selectedItem = (String)mAdapter.getItem(position);
            Log.e("search view", selectedItem);
            return true;
        }

但是我有一个错误:android.database.MatrixCursor cannot be cast to java.lang.String 而且我不确定如何处理它。非常感谢任何形式的帮助。

最佳答案

如果有人还在寻找答案。使用此代码。

searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {

    @Override
    public boolean onSuggestionSelect(int position) {
        return true;
    }

    @Override
    public boolean onSuggestionClick(int position) {
        Cursor cursor= searchView.getSuggestionsAdapter().getCursor();
        cursor.moveToPosition(position);
        String suggestion =cursor.getString(2);//2 is the index of col containing suggestion name.
        searchView.setQuery(suggestion,true);//setting suggestion
        return true;
    }
});

关于android - SearchView:从建议监听器中获取选中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30831484/

相关文章:

android - NPE 自定义 ListView 适配器

Android Listview - 使用光标时无法选择多个项目

android - SimpleCursorAdapter 和 ViewBinder - 将数据绑定(bind)到要在单击时检索的 ListView 项目

android - 检测何时开始在其他应用程序中播放音频

java - 使用连接多个 View 的单个手势检测器获得双击/触摸 View

android - 为什么不能将多个 View 绑定(bind)到一个所有者?

android - 如何为 ListView 中的每一行设置不同的高度?

android - 未弃用的包装选项?

android - ListFragment 中没有加载圆圈

android - 从其 ListAdapter 的行 ID 获取 ListView 项目 ID