我的应用程序中有两个带有单独 ListView 的适配器 一个用于用户,另一个用于组 我想在条件存在时将适配器设置为 TextView ,例如当用户键入 token “@”时,应设置用户适配器,当用户键入 token “#”时, channel 适配器(用于填充组列表)应为放 我的适配器:
final ChannelAdapter customadapter1 = new ChannelAdapter(getActivity(), R.layout.all_cahnnel_list_item, joinOtherChannelList);
final UserAdapter customadapter = new UserAdapter(getActivity(), R.layout.all_user_list_item, userArrayList);
inputMessageView = (MultiAutoCompleteTextView) view.findViewById(R.id.message_input);
inputMessageView.setThreshold(0);
inputMessageView.setTokenizer(new MultiAutoCompleteTextView.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 + " ";
}
}
}
@Override
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (((i > 0 && text.charAt(i - 1) != '@') && (i > 0 && text.charAt(i - 1) != '#'))) {
i--;
}//Check if token really started with @, else we don't have a valid token
if (text.charAt(i - 1) != '@') {
inputMessageView.setAdapter(customadapter);
返回光标: } else if (i < 1 || text.charAt(i - 1) != '#') { inputMessageView.setAdapter(customadapter1); 返回光标;
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;
}
});
我的问题是当我单独使用 if 条件时它适用于自定义适配器一。但是当我使用 if 和 else if 来设置两个适配器时,甚至单个适配器都无法正常工作,我的意思是它没有在 TextView 中显示任何内容
最佳答案
你们的条件不一样。
if (text.charAt(i - 1) != '@') {
inputMessageView.setAdapter(customadapter)
return cursor;
} else if (i < 1 || text.charAt(i - 1) != '#') {
inputMessageView.setAdapter(customadapter2);
return cursor;
}
If ,如您所说,在一个 if 条件下工作,那么它应该工作,因为 else if 部分没有被执行。尝试使用日志并尝试打印日志。
关于android - 如何根据 if 条件为 AutocompleteTextview 设置两个适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780047/