AutoCompleteTextView 上的 getText() 返回 SpannableStringBuilder?

发布于 2024-09-27 15:59:05 字数 1248 浏览 5 评论 0原文

我有一个带有 MatrixCursor 作为适配器的 AutoCompleteTextView。 AutoCompleteTextView 有一个 OnItemClickListener,如下所示:

public class AutoCompleteListClickListner implements OnItemClickListener
{
    private AutoCompleteTextView acView;

    public AutoCompleteListClickListner(AutoCompleteTextView view)
    {
        this.acView = view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        /* tv is the selected view in the dropdown */
        TextView tv = (TextView) ((LinearLayout) view).getChildAt(0);
        Log.d(AndroidLogTag, "Before:>>>>" + this.acView.getText().toString());
        this.acView.setText(tv.getText().toString());
        Log.d(AndroidLogTag, "After:>>>>" + this.acView.getText().toString());
    }
}

当选择一个项目时,我在日志中看到以下内容

10-14 00:34:37.893: DEBUG/MyApp(19151): Before:>>>>android.database.MatrixCursor@4643e388
10-14 00:34:37.912: DEBUG/MyApp(19151): After:>>>>Ankit V Jain

如果您看到日志,Before 日志条目,getText() 返回一个SpannableStringBuilder 对象及其 toString() 返回一些对象标识符。

如何获取单击下拉项之前存在的自动完成视图的实际文本?

I have an AutoCompleteTextView with a MatrixCursor as an adapter. The AutoCompleteTextView has a OnItemClickListener as follows:

public class AutoCompleteListClickListner implements OnItemClickListener
{
    private AutoCompleteTextView acView;

    public AutoCompleteListClickListner(AutoCompleteTextView view)
    {
        this.acView = view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        /* tv is the selected view in the dropdown */
        TextView tv = (TextView) ((LinearLayout) view).getChildAt(0);
        Log.d(AndroidLogTag, "Before:>>>>" + this.acView.getText().toString());
        this.acView.setText(tv.getText().toString());
        Log.d(AndroidLogTag, "After:>>>>" + this.acView.getText().toString());
    }
}

When an item is selected I see following in the log

10-14 00:34:37.893: DEBUG/MyApp(19151): Before:>>>>android.database.MatrixCursor@4643e388
10-14 00:34:37.912: DEBUG/MyApp(19151): After:>>>>Ankit V Jain

If you see the log, the Before log entry, the getText() returns a SpannableStringBuilder object and toString() of it returns some object identifier.

How do I get actual text of the auto-complete view which was there just before clicking drop-down item?

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

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

发布评论

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

评论(2

樱桃奶球 2024-10-04 15:59:05

尝试将 SpannableStringBuilder 转换为 CharSequence

try casting SpannableStringBuilder to CharSequence

巾帼英雄 2024-10-04 15:59:05

好吧,代码中总是有另一种方式(肮脏的或最好的)。

我向 AutoCompleteView 添加了一个 TextWatcher ,并使用 beforeTextChanged() 事件来记录文本内容并在 onItemClick() 中重用它!

Well, there is always another way (dirty or best) in code.

I added a TextWatcher to the AutoCompleteView and used beforeTextChanged() event to record text content and reuse that in onItemClick() !

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