将 Android 自动完成中的选择转换为字符串

发布于 2024-12-04 04:17:25 字数 590 浏览 1 评论 0原文

我刚刚使用在线网络服务实现了文本字段的自动完成,基于 Stackoverflow 上的这个答案: ArrayAdapter 从 AutoCompleteTextAdapter 中的 Web 服务更新

使用 ArrayAdapter<用户>;实现了 Filterable,我已经设法让自动完成功能按预期建议我输入条目。 User 是一个 Java Bean,其中包含建议中提供的信息(年龄、姓名等)。

当我选择建议时,自动完成字段将使用“错误”数据填充 - 使用 toString() 方法,而不是 bean 的“名称”属性。

我的问题是:我可以重写(在适配器中)一个方法,该方法将允许我指定如何转换 bean 以便为 AutoCompleteTextView 返回正确的属性吗? (理想情况下,不应更改 User.toString())

谢谢!

I have just implemented an autocompletion for a textfield using an online webservice, based on this answer on Stackoverflow:
ArrayAdapter is updated late from Webservice in AutoCompleteTextAdapter

Using an ArrayAdapter<User> implements Filterable, I have managed that the autocompletion suggests me entries as intended.
User is a Java Bean that contains information which is presented in the suggestion (age, name, ...).

When I select a suggestion, the Autocomplete field is filled with the 'wrong' data - using the toString()method, instead of the 'name' property of the bean.

My Question is: Can I override (in the Adapter) a method which will allow me to specify how to convert the bean so that the correct property is returned for the AutoCompleteTextView?
(Ideally, User.toString() should not be changed)

Thx!

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

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

发布评论

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

评论(4

久光 2024-12-11 04:17:25

无需子类化 AutoCompleteTextView 并重写 convertSelectionToString 方法。通过覆盖 convertResultToString 方法位于(您已经子类化的)ArrayAdapter 中的自定义Filter 中。

我遇到了同样的问题:我的 ArrayAdapter 中的自定义对象的 toString() 实现不是我可以控制的。我实现了这样的方法:

// In custom Filter implementation

@Override
public CharSequence convertResultToString(Object result) {
   if(result instanceof MyCustomClass) {
      return ((MyCustomClass) result).getAttribute("name");
   }

   return super.convertResultToString(result);
}

There is no need subclassing AutoCompleteTextViewand override the convertSelectionToStringmethod. The same thing is achievable by overriding the convertResultToStringmethod in your custom Filter in (your already subclassed) ArrayAdapter.

I had the same problem: custom objects in my ArrayAdapter whose toString() implementation wasn't something I could control. I implemented the method like this:

// In custom Filter implementation

@Override
public CharSequence convertResultToString(Object result) {
   if(result instanceof MyCustomClass) {
      return ((MyCustomClass) result).getAttribute("name");
   }

   return super.convertResultToString(result);
}
深府石板幽径 2024-12-11 04:17:25

搜索结果取决于数据的 toString() 返回的内容。在您的情况下,您需要在 toString() 实现中返回 name 字段。

The search results depend on what the data's toString() returns. In your case you need to return name field in the toString() implementation.

卷耳 2024-12-11 04:17:25

我找到了另一种方法:

AutoCompleteTextView 中的方法 convertSelectionToString(Object selectedItem) 可以被子类覆盖以允许自定义转换。这样,就不需要对 toString() 方法进行调整。

在我看来,这样做的优点是过滤器不仅可以返回字符串列表,还可以返回自定义对象列表,这些对象可以由 getView(intposition, View ConvertView, ViewGroupparent) 使用 适配器构建“更丰富”的建议。

明显的缺点是,它需要为每个不能修改 toString() 方法的 Filterresult 类型子类化 AutoCompleteTextView。

@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
    if(selectedItem instanceof User){
        User u = (User) selectedItem;
        return u.getUsername();
    } else {
        return super.convertSelectionToString(selectedItem);
    }
}

对此有何评论?

I have found another way:

The method convertSelectionToString(Object selectedItem) in AutoCompleteTextView can be overridden by subclasses to allow for custom conversions. This way, no adjustment to the toString() method is required.

This - it seems to me - has the advantage that the Filter can return not just a list of Strings but a list of custom objects, which can be used by getView(int position, View convertView, ViewGroup parent) of the adapter to construct "richer" suggestions.

The obvious disadvantage is that it requires subclassing AutoCompleteTextView for every Filterresult type whose toString() method shall not be modified.

@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
    if(selectedItem instanceof User){
        User u = (User) selectedItem;
        return u.getUsername();
    } else {
        return super.convertSelectionToString(selectedItem);
    }
}

Any comments on this?

贱人配狗天长地久 2024-12-11 04:17:25

如果您从 SimpleCursorAdapter 继承您自己的适配器,则可以在构造函数中的 Adapter 上设置 CursorToStringConverter

private class AutoCompleteAdapter extends SimpleCursorAdapter {

    public AutoCompleteAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        /* Other setup code here  */

        setCursorToStringConverter(new CursorToStringConverter() {
            @Override
            public CharSequence convertToString(Cursor item) {
                return item.getString(item.getColumnIndex(DESIRED_COLUMN_NAME));
            }
        });
    }
}

If you subclass your own adapter from SimpleCursorAdapter, you can set a CursorToStringConverter on the Adapter in the constructor.

private class AutoCompleteAdapter extends SimpleCursorAdapter {

    public AutoCompleteAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        /* Other setup code here  */

        setCursorToStringConverter(new CursorToStringConverter() {
            @Override
            public CharSequence convertToString(Cursor item) {
                return item.getString(item.getColumnIndex(DESIRED_COLUMN_NAME));
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文