android autocompletetextview 应仅在下拉列表中显示相关选项

发布于 2024-11-01 08:33:50 字数 766 浏览 6 评论 0 原文

我在代码中使用 AutoCompleteTextView 并使用 SimpleCursorAdapter 从数据库加载列表。

AutoCompleteTextView cocktailIngredientView = (AutoCompleteTextView) findViewById(R.id.item);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item, mCursor,
                new String[] { "field" },
                new int[] { android.R.id.text1 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cocktailIngredientView.setAdapter(adapter);
cocktailIngredientView.setThreshold(0);

它正确填充列表,但我有两个问题:

  1. 我希望对该列表进行排序
  2. 无论我输入什么,它都会显示完整的列表。我希望它根据列表中的匹配模式进行过滤。例如,如果列表包含值“页面”、“工具”...那么如果我在框中输入 T,则下拉列表应仅显示“工具”。这个想法是显示在字符串文本中的任何位置包含输入模式的选项。

这怎么能做到呢?

I am using a AutoCompleteTextView in my code and loading the list from database using SimpleCursorAdapter.

AutoCompleteTextView cocktailIngredientView = (AutoCompleteTextView) findViewById(R.id.item);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item, mCursor,
                new String[] { "field" },
                new int[] { android.R.id.text1 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cocktailIngredientView.setAdapter(adapter);
cocktailIngredientView.setThreshold(0);

It populates the list correctly but I have two issues:

  1. I want this list to be sorted
  2. Whatever I enter, it displays the complete list. I want it to filter based on matching patterns in the list. e.g. if the list contains values Page, Tools...then if I enter T in the box, the drop-down should show only Tools. The idea is to display options which contain the entered pattern anywhere in the string text.

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-11-08 08:33:50

您必须告诉适配器要显示哪些项目。我尝试使用 FilterQueryProvider< 来实现类似的东西/a> 在数据库中查询我想要在下拉列表中显示的项目。

FilterQueryProvider filter = new FilterQueryProvider() {

    @Override
    public Cursor runQuery(CharSequence constraint) {
        // Make a DB query that filters based on the constraint

        return //whatever query results;
    }
};
myAdapter.setFilterQueryProvider(filter);

至于当你在列表中选择一个项目时的情况,你必须重写 SimpleCursorAdapter 的 CursorToStringConverter。像这样的东西:

SimpleCursorAdapter.CursorToStringConverter conv = new SimpleCursorAdapter.CursorToStringConverter() {

    @Override
    public CharSequence convertToString(Cursor cursor) {
        int numCol = cursor.getColumnIndexOrThrow("whateverFieldYouNeed");
        String term = cursor.getString(numCol);
        return term;
    }
};
myAdapter.setCursorToStringConverter(conv);

You have to tell the adapter what items to display. I tried implementing something similar to this by using a FilterQueryProvider that queries the database for the items that I want to display in the dropdown.

FilterQueryProvider filter = new FilterQueryProvider() {

    @Override
    public Cursor runQuery(CharSequence constraint) {
        // Make a DB query that filters based on the constraint

        return //whatever query results;
    }
};
myAdapter.setFilterQueryProvider(filter);

As for the situation when you select an item on the list, you have to override the CursorToStringConverter of the SimpleCursorAdapter. Something like:

SimpleCursorAdapter.CursorToStringConverter conv = new SimpleCursorAdapter.CursorToStringConverter() {

    @Override
    public CharSequence convertToString(Cursor cursor) {
        int numCol = cursor.getColumnIndexOrThrow("whateverFieldYouNeed");
        String term = cursor.getString(numCol);
        return term;
    }
};
myAdapter.setCursorToStringConverter(conv);
溺深海 2024-11-08 08:33:50

除了 CursorToStringConverter 您也可以使用

mAdapter.setStringConversionColumn(mCursor.getColumnIndexOrThrow("whateverFieldYouNeed"));

Instead of the CursorToStringConverter you could also use

mAdapter.setStringConversionColumn(mCursor.getColumnIndexOrThrow("whateverFieldYouNeed"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文