带有 SimpleCursorAdapter 的 AutoCompleteTextView 不过滤

AutoCompleteTextView with SimpleCursorAdapter does not filter

在我的应用程序中,我有几个使用 ArrayAdapter 的 AutoCompleteTextView 小部件。

private List<String> adapterList = new ArrayList<String>();
ArrayAdapter<String> dropdownAdapter;
dropdownAdapter = new ArrayAdapter<>(getContext(), R.layout.simple_dropdown_item, adapterList);
autoCompleteTextView.setAdapter(dropdownAdapter);

效果很好。当我在视图中键入内容时,我会在下拉列表中看到以单词开头的结果。

我想使用另一个 AutoCompleteTextView 来完成此操作,但这次使用的是 SimpleCursorAdapter。

nameSearchCursor = dbHelper.getChecklistTabDataByChecklistId(outingId, checklistId, nameColumn);
NameSearch = root.findViewById(R.id.SearchNames);
String[] nsColumns = new String[]{nameColumn};
int[] nsTo = new int[]{R.id.simpleDropdownItem};
nameSearchCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.simple_dropdown_item,
        nameSearchCursor, nsColumns, nsTo, 0);
NameSearch.setAdapter(nameSearchCursorAdapter);

如果我开始在此新视图中键入内容,则会出现下拉列表并显示整个列表,并且在我键入时没有任何变化。没有过滤发生。我需要做些什么不同的事情(也许为什么)才能让 CursorAdapter 与我在使用 ArrayAdapter 时不需要做的视图一起工作。我搜索了这个网站并阅读了开发者文档,一定有一些我不明白的地方。请赐教。

这个网站让我回答了这个问题:http://www.outofwhatbox.com/blog/2010/11/android-simpler-autocompletetextview-with-simplecursoradapter/

这是我完成的代码:

private void setUpNameSearch() {
    // Get AutoCompleteTextView
    nameSearchView = root.findViewById(R.id.SearchNames);
    // Define from/to info
    final String[] nsColumns = new String[]{nameColumn};
    final int[] nsTo = new int[]{R.id.simpleDropdownItem};
    // Create adapter. Cursor set in setFilterQueryProvider() below.
    nameSearchCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.simple_dropdown_item,
            null, nsColumns, nsTo, 0);
    // Set adapter on view.
    nameSearchView.setAdapter(nameSearchCursorAdapter);

    // OnItemClickListener - User selected value from DropDown
    nameSearchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            // Get the cursor. Positioned to the corresponding row in the result set.
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);
            // Get the name selected
            String selectedName = cursor.getString(cursor.getColumnIndexOrThrow(nameColumn));
            // Do something with this value...
        }
    });

    // Set the CursorToStringconverter, to provide the values for the choices to be displayed
    // in the AutoCompleteTextview.
    nameSearchCursorAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
        @Override
        public CharSequence convertToString(Cursor cursor) {
            final String name = cursor.getString(cursor.getColumnIndexOrThrow(nameColumn));
            return name;
        }
    });

    // Set the FilterQueryProvider, to run queries for choices
    nameSearchCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            Cursor cursor = dbHelper.getMatchingNames(outingId, checklistId, nameColumn,
                    (constraint != null ? constraint.toString() : null));
            return cursor;
        }
    });
}

我想使用 SQLite Cursor 复制 AutoCompeteTextView 的以单词开头的默认功能,却发现不完全支持 REGEXP。所以这个 Whosebug 主题给了我 LIKE 解决方法。 SQLite LIKE alternative for REGEXP, Match Start of Any Word

我希望这对其他人有帮助。