带自定义适配器的MultiAutoCompleteTextView显示乱码字符串

Sid*_*ele 6 android baseadapter

这是我之前的问题的后续跟进:Android:自动完成TextView类似于Facebook应用程序.

的背景:

我在问题中的要求(上面发布的链接)AutoCompleteTextView与Facebook应用程序中使用的类似,其他几个也是如此.解决方案是使用多线路.MultiAutoCompleteTextView这个想法是让用户在创建状态更新时直接输入他们的好友名称.答案中的解决方案从独立的角度来看很好.但是,当我盯着在现有代码中集成解决方案时,它仍然适用于正确的下拉列表等等.感谢来自这里的解决方案,我看到了我朋友的过滤列表:https://stackoverflow.com/a/12363961/1350013.我用一个自定义的ListView一个BaseAdapter,而不是GridView从溶液中.

问题:

我的代码使用了一个BaseAdapter实现Filterable.如上所述,工作正常.

当我从筛选列表中选择一个朋友时,问题出在哪里.的MultiAutoCompleteTextView,选择后,显示此:@MY_PACKAGE_NAME.Friends.getFriends@406c1058而不是朋友的名字.我需要更改什么来显示名称而不是乱码文本?如果它有帮助,那么我在第一类中运行它来扩展a SherlockActivity而不是a SherlockListActivity.

现在我不确定相关代码是什么,以找出问题所在的位置,因此我将尽可能多地发布相关代码.我是一个菜鸟,所以请轻松并要求任何额外的代码.我会及时遵守.同样,如果不需要这里的东西使帖子混乱,我将删除它.

代码块

来自我之前的问题的解决方案中的Tokenizer.链接在顶部(在onCreate()方法中)

editStatusUpdate = (MultiAutoCompleteTextView) findViewById(R.id.editStatusUpdate);
editStatusUpdate.addTextChangedListener(filterTextWatcher);

editStatusUpdate.setTokenizer(new Tokenizer() {

    @Override
    public CharSequence terminateToken(CharSequence text) {

        int i = text.length();

        while (i > 0 && text.charAt(i - 1) == ' ') {
            i--;
        }

        if (i > 0 && text.charAt(i - 1) == ' ') {
            return text;
        } else {
            if (text instanceof Spanned) {
                SpannableString sp = new SpannableString(text + " ");
                TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
                return sp;
            } else {
                return text.toString() + " ";
            }
        }
    }

    @Override
    public int findTokenStart(CharSequence text, int cursor) {

        int i = cursor;

        while (i > 0 && text.charAt(i - 1) != '@') {
            i--;
        }

        if (i < 1 || text.charAt(i - 1) != '@') {
            return cursor;
        }

        return i;
    }

    @Override
    public int findTokenEnd(CharSequence text, int cursor) {

        int i = cursor;
        int len = text.length();

        while (i < len) {
            if (text.charAt(i) == ' ') {
                return i;
            } else {
                i++;
            }
        }

        return len;

    }
});
Run Code Online (Sandbox Code Playgroud)

TextWatcher,也是前面问题的解决方案:

private TextWatcher filterTextWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        Layout layout = editStatusUpdate.getLayout();
        int pos = editStatusUpdate.getSelectionStart();
        int line = layout.getLineForOffset(pos);
        int baseline = layout.getLineBaseline(line);

        int bottom = editStatusUpdate.getHeight();

        editStatusUpdate.setDropDownVerticalOffset(baseline - bottom);

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
Run Code Online (Sandbox Code Playgroud)

对于我的ArrayList:

public class getFriends {

    String friendID;
    String friendName;
    String friendProfile;

    boolean selected;

    // SET FRIENDS ID
    public void setFriendID(String friendID) {
        this.friendID = friendID;
    }

    // GET FRIENDS ID
    public String getFriendID() {
        return friendID;
    }

    // SET FRIENDS NAME
    public void setFriendName(String friendName) {
        this.friendName = friendName;
    }

    // GET FRIENDS NAME
    public String getFriendName() {
        return friendName;
    }

    // SET FRIENDS PROFILE
    public void setFriendProfile(String friendProfile) {
        this.friendProfile = friendProfile;
    }

    // GET FRIENDS PROFILE
    public String getFriendProfile() {
        return friendProfile;
    }
}
Run Code Online (Sandbox Code Playgroud)

Hei*_*sch 14

您应该convertResultToString(Object resultValue)在过滤器中覆盖,其中Object是您的类.这样就可以为您的类创建自定义字符串,而无需依赖覆盖to String方法.

  • 这应该是公认的答案.由于您不需要在模型对象中获取应用程序上下文,因此在国际化方面效果更好. (2认同)

Luk*_*rog 6

根据您得到的输出判断,您的代码会调用对象toString上的方法getClass来检索所需的数据.如你想要朋友的名字,你应该实现自己的toString方法(通过覆盖它),如下所示:

@Override
public String toString() {
    return friendName; 
}
Run Code Online (Sandbox Code Playgroud)