MultiAutoCompleteTextView与自定义适配器显示乱码字符串 [英] MultiAutoCompleteTextView with Custom Adapter displays garbled string
问题描述
这是一个跟进我的问题更早这里:安卓:自动完成的TextView类似于Facebook应用程序
背景:的
我在这个问题(上面的链接贴)要求是有一个 AutoCompleteTextView
类似于Facebook应用程序和其他几个人太习惯之一。解决的办法是使用多行 MultiAutoCompleteTextView
当时的想法是让用户在创建状态更新直接键入他们的朋友的名字。在答案的解决方案,工作正常,从一个独立的点。然而,当我盯着整合在我的现有code中的解决方案,但它仍然有正确的下拉列表等所有工作。我看到我的朋友们感谢过滤列表,从这里一个解决方案: http://stackoverflow.com/a/12363961/1350013一>。我使用自定义的的ListView
用的 BaseAdapter
不是的GridView
从溶液中。
的问题:的
我的code,能使用 BaseAdapter
它实现可筛选
。正如上面提到的,工作正常。
当我从过滤后的列表中选择一个朋友,就是问题的所在。该 MultiAutoCompleteTextView
,选择后,显示如下: @ MY_PACKAGE_NAME.Friends.getFriends @ 406c1058
而不是朋友的名字。什么我不得不改变的乱码文字显示的名称呢?如果有帮助,I类运行这扩展了 SherlockActivity
,而不是 SherlockListActivity
。
现在我不知道是什么的相关code是要找到问题出在哪里可能在于,所以我将发布尽可能多的相关code越好。我是小白,所以请轻松,并要求任何额外的code。我会及时履行。同样,如果在这里的东西是没有必要的混乱后,我将删除。
code幢的
标记生成器的从我刚才的问题解决方案。在顶部链接(在的onCreate()
法)
editStatusUpdate =(MultiAutoCompleteTextView)findViewById(R.id.editStatusUpdate);
editStatusUpdate.addTextChangedListener(filterTextWatcher);
editStatusUpdate.setTokenizer(新标记生成器(){
@覆盖
公共CharSequence的terminateToken(CharSequence的文字){
INT I = text.length();
而(ⅰ大于0&安培;&安培; text.charAt(ⅰ - 1)==''){
一世 - ;
}
如果(ⅰ大于0&安培;&安培; text.charAt(ⅰ - 1)==''){
返回文本;
} 其他 {
如果(文本的instanceof跨区){
SpannableString SP =新SpannableString(文字+);
TextUtils.copySpansFrom((跨区)文本,0,text.length(),Object.class,SP,0);
返回SP;
} 其他 {
返回text.toString()+;
}
}
}
@覆盖
公众诠释findTokenStart(CharSequence的文字,诠释光标){
INT I =光标;
而(ⅰ大于0&安培;&安培;!text.charAt(ⅰ - 1)=@){
一世 - ;
}
如果(I<!1 || text.charAt(I - 1)='@'){
返回游标;
}
返回我;
}
@覆盖
公众诠释findTokenEnd(CharSequence的文字,诠释光标){
INT I =光标;
INT的len = text.length();
而(I< LEN){
如果(text.charAt(ⅰ)==''){
返回我;
} 其他 {
我++;
}
}
返回的len;
}
});
的TextWatcher,还从溶液到前面的问题:
私人TextWatcher filterTextWatcher =新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
布局布局= editStatusUpdate.getLayout();
INT POS = editStatusUpdate.getSelectionStart();
INT线= layout.getLineForOffset(POS);
INT基线= layout.getLineBaseline(线);
INT底部= editStatusUpdate.getHeight();
editStatusUpdate.setDropDownVerticalOffset(基线 - 底);
}
@覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
}
@覆盖
公共无效afterTextChanged(编辑S){
}
};
有关我的ArrayList
公共类getFriends {
字符串friendID;
字符串friendName;
字符串friendProfile;
布尔选择;
//设置的朋友ID
公共无效setFriendID(字符串friendID){
this.friendID = friendID;
}
//得到朋友ID
公共字符串getFriendID(){
返回friendID;
}
//设置朋友的名字
公共无效setFriendName(字符串friendName){
this.friendName = friendName;
}
//获取朋友的名字
公共字符串getFriendName(){
返回friendName;
}
//设置好友资料
公共无效setFriendProfile(字符串friendProfile){
this.friendProfile = friendProfile;
}
//获取好友资料
公共字符串getFriendProfile(){
返回friendProfile;
}
}
由输出看你看来,你的code呼吁的toString
方法你的getClass
对象来获取需要的数据。当你想要的朋友的名字,你应该实现自己的的toString
方法(通过重写它)是这样的:
@覆盖
公共字符串的toString(){
返回friendName;
}
This is a follow up to my question earlier here: Android: Autocomplete TextView Similar To The Facebook App.
The Background:
My requirement in the question (link posted above) was to have an AutoCompleteTextView
similar to the one used in the Facebook app and several others too. The solution was to use a multi-line MultiAutoCompleteTextView
The idea was to enable users to type their Friends names directly while creating a Status Update. The solution in the answer works fine from a standalone point of view. However, when I stared integrating the solution in my existing code, it still works with the correct drop-down et all. I see the filtered list of my friends thanks to a solution from here: http://stackoverflow.com/a/12363961/1350013. I use a custom ListView
with a BaseAdapter
instead of the GridView
from the solution.
The Problem:
My code makes use of a BaseAdapter
which implements Filterable
. As mentioned above, works fine.
When I select a Friend from the filtered list, is where the problem is. The MultiAutoCompleteTextView
, after selection, displays this: @MY_PACKAGE_NAME.Friends.getFriends@406c1058
instead of the Friend's name. What would I have to change to show the Name instead of the garbled text? If it helps, the Class I run this in extends a SherlockActivity
and not a SherlockListActivity
.
Now I am not sure what the relevant code would be to find out where the problem might lie, so I will post as much relevant code as possible. I am a noob, so please be easy and ask for any additional code. I will promptly comply. Likewise, if something here is not needed is cluttering the post, I will remove that.
CODE BLOCKS
The Tokenizer from the solution from my earlier question. Linked at the top (In the onCreate()
method)
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;
}
});
The TextWatcher, also from the solution to the earlier question:
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) {
}
};
For my 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;
}
}
Judging by the output you get it appears that your code calls the toString
method on your getClass
objects to retrieve the data it needs. As you want the friend's name you should implement your own toString
method(by overriding it) like this:
@Override
public String toString() {
return friendName;
}
这篇关于MultiAutoCompleteTextView与自定义适配器显示乱码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!