使用 AutoCompleteTextView 自定义光标到文本

发布于 2024-10-03 12:08:07 字数 1849 浏览 0 评论 0原文

我通过扩展 AutoCompleteTextView (使用名为 setCursorAdapter 的专用函数)创建了一个自定义视图。我提供 SimpleCursorAdapter 并添加 FilterQueryProvider。我将其用于数字列表,我需要能够在数字中的任何位置搜索匹配项。

public void setCursorAdapter(final Uri uri, final String key) {

        Cursor c = getContext().getContentResolver().query(uri,
                new String[] { "_id", key }, null, null, key);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(),
                android.R.layout.simple_dropdown_item_1line, c,
                new String[] { key }, new int[] { android.R.id.text1 });

        this.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                ((SimpleCursorAdapter) getAdapter()).getFilterQueryProvider()
                        .runQuery(s);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        adapter.setFilterQueryProvider(new FilterQueryProvider() {

            @Override
            public Cursor runQuery(CharSequence constraint) {
                Cursor c = getContext().getContentResolver().query(uri,
                        new String[] { "_id", key },
                        key + " LIKE '%" + constraint + "%'", null, key);
                return c;
            }
        });
        setAdapter(adapter);
    }

一切都工作正常,直到我从下拉列表中选择值。我在 EditText 窗口中收到的值: android.content.ContentResolver$CursorWrapperInner@...

如何避免这种情况并让我的(数字)文本正确显示。

谢谢

I have created a custom view by extending AutoCompleteTextView (with a specialized function called setCursorAdapter). I am supplying a SimpleCursorAdapter and adding a FilterQueryProvider. I am using this for a list of numbers where I need the ability to search anywhere within the number for a match.

public void setCursorAdapter(final Uri uri, final String key) {

        Cursor c = getContext().getContentResolver().query(uri,
                new String[] { "_id", key }, null, null, key);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(),
                android.R.layout.simple_dropdown_item_1line, c,
                new String[] { key }, new int[] { android.R.id.text1 });

        this.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                ((SimpleCursorAdapter) getAdapter()).getFilterQueryProvider()
                        .runQuery(s);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        adapter.setFilterQueryProvider(new FilterQueryProvider() {

            @Override
            public Cursor runQuery(CharSequence constraint) {
                Cursor c = getContext().getContentResolver().query(uri,
                        new String[] { "_id", key },
                        key + " LIKE '%" + constraint + "%'", null, key);
                return c;
            }
        });
        setAdapter(adapter);
    }

Everything is working perfectly, until I select the value from the drop down. The value that I receive in the EditText window: android.content.ContentResolver$CursorWrapperInner@...

How do I avoid this and get my (numeric) text displayed correctly.

Thanks

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

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

发布评论

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

评论(2

俯瞰星空 2024-10-10 12:08:07

已解决:SimpleCursorAdapter.converToString(cursor) - 我重写了此函数以提取值。

谢谢

Solved: SimpleCursorAdapter.converToString(cursor) -- I overrode this function to extract the value.

Thanks

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