CursorAdapter不能填充SearchView的建议列表

3

我有一个String元素的数组:

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;
        }
    });

然而,当我开始在搜索框中输入我的建议数组的元素时,我没有得到任何建议。我进行了多次调试,以查看是否有任何代码未按时/未按顺序被调用,但一切都运行良好。我的数组元素不包含任何奇怪的字母、符号等。我错过了什么?谢谢。

1个回答

1
Snap!我忘记设置适配器了。
searchView.setSuggestionsAdapter(cursorAdapter);

新手错误,不管怎样,也许有人会发现代码和解决方法有用。

1
我读了你的答案,嗤之以鼻,然后意识到我也忘记了setSuggestionsAdapter... - ning

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接