【问题标题】:CursorAdapter not populating the suggestions of SearchViewCursorAdapter 未填充 SearchView 的建议
【发布时间】:2016-07-13 00:12:01
【问题描述】:

我有一个字符串元素数组:

suggestion_name = new String[main_data.getJSONArray("cafe").length()];

由一些 JSON 数据填充:

        for (int i=0; i<suggestion_name.length;i++){

        suggestion_name[i] = main_data.getJSONArray("cafe").getJSONObject(i).getString("name");

        }

当我调试时,我看到Suggestion_name 数组中有2 个元素,这里一切正常。然后我按如下方式创建我的建议适配器:

public void set_suggestion_adapter_array(){
    String[] from = new String[] {"cafe_name"};
    int[] to = new int[] {R.id.suggestionTextView};
    cursorAdapter = new SimpleCursorAdapter(a,
            R.layout.suggestions_single_item,
            null,
            from,
            to,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}

并像这样填充建议列表:

public void populate(String text){
    MatrixCursor matrixCursor = new MatrixCursor(new String[]{ BaseColumns._ID, "cafe_name" });
    for (int i=0; i<suggestion_name.length; i++) {
        if (suggestion_name[i].toLowerCase().startsWith(text.toLowerCase()))
            matrixCursor.addRow(new Object[] {i, suggestion_name[i]});
    }
    cursorAdapter.changeCursor(matrixCursor);
    cursorAdapter.notifyDataSetChanged();
}

我在 onQueryTextChange 中调用 populate() 方法:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // search database for query
            getDataMap.search(query);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // show suggestions
            getDataMap.populate(newText);

            return true;
        }
    });

然而,当我开始在搜索字段中输入建议数组的元素时,我没有收到任何建议。我调试了几次以查看我的任何代码是否没有及时/根本没有被调用,但一切都按顺序正常运行。我的数组元素不包含任何奇怪的字母、符号等。我在这里缺少什么?谢谢。

【问题讨论】:

    标签: android adapter searchview android-cursoradapter search-suggestion


    【解决方案1】:

    快点!我忘了设置适配器。

    searchView.setSuggestionsAdapter(cursorAdapter);
    

    我的菜鸟错误。无论如何,也许有人发现代码和解决方法很有用。

    【讨论】:

    • 我看了你的回答,嗤之以鼻,然后意识到我也忘了setSuggestionsAdapter...
    猜你喜欢
    • 2017-04-27
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多