我想在我的 android 应用程序中实现用户标记功能。我想要 Facebook 在评论中带有用户标签的相同功能。为此,我MultiAutoCompleteTextView
在我的应用程序中实现了自定义,并且我还想同时显示用户图像和用户名。为此,我为 MultiAutoCompleteTextView 实现了自SimpleAdapter
定义Filter
。我参考了此链接并实现了该功能。
我要实现的更改是,每当我从下拉列表中选择任何项目时,都应从下拉列表中删除该项目,并且应使用剩余数据更新下拉数据。如果我从 AutocompleteText 中明确删除该选定项目,则该已删除项目应重新出现在下拉选择框中。我不知道如何实现这一点。我搜索了很多,但可以得到任何适当的解决方案。请帮我解决这个问题。
谢谢你。
活动代码:
edtCommentTag.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 + " ";
}
}
}
@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;
}
});
adpReply = new CommentAdapter(CommentActivity.this,
finalResult, R.layout.liker_comment_row, from, to);
edtCommentTag.setAdapter(adpReply);
评论适配器代码:
public class CommentReplyAdapter extends SimpleAdapter {
private List<HashMap<String, String>> lstFollowing, suggestions,
tempLstFollowing;
RoundedImageView imgUser;
TextView txtUname;
private LayoutInflater mInflater;
Context context;
private ImageLoader imgLoader;
private DisplayImageOptions options;
private Filter nameFilter;
public CommentReplyAdapter(Context context,
List<HashMap<String, String>> lstFollowing, int lytId,
String[] from, int[] to) {
super(context, lstFollowing, lytId, from, to);
this.context = context;
this.lstFollowing = lstFollowing;
this.tempLstFollowing = new ArrayList<HashMap<String, String>>();
this.tempLstFollowing.addAll(lstFollowing);
this.suggestions = new ArrayList<HashMap<String, String>>();
this.mInflater = LayoutInflater.from(context);
imgLoader = ImageLoader.getInstance();
imgLoader.init(ImageLoaderConfiguration.createDefault(this.context));
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading_img)
.showImageForEmptyUri(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.loading_img).cacheInMemory(true)
.cacheOnDisc(true).considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.resetViewBeforeLoading(true).build();
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.liker_comment_row,
null);
}
txtUname = (TextView) convertView
.findViewById(R.id.txtUserName_likeComment);
imgUser = (RoundedImageView) convertView
.findViewById(R.id.userImage_likeComment);
HashMap<String, String> data = (HashMap<String, String>) getItem(position);
txtUname.setText(data.get("uname"));
imgLoader.displayImage(data.get("avtar"), imgUser, options);
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public Filter getFilter() {
setUpFilter();
return nameFilter;
}
private void setUpFilter() {
try {
nameFilter = new Filter() {
@SuppressWarnings("unchecked")
@Override
public CharSequence convertResultToString(Object resultValue) {
String str = ((HashMap<String, String>) resultValue)
.get("uname");
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
String newConstraint = constraint.toString();
Log.e("newConstraint -- outer", newConstraint);
suggestions.clear();
for (HashMap<String, String> hm : tempLstFollowing) {
String uname = hm.get("uname").toString()
.toLowerCase(Locale.ENGLISH);
if (uname.startsWith(newConstraint.toString()
.toLowerCase(Locale.ENGLISH))) {
suggestions.add(hm);
// hasFound = true;
}
}
FilterResults filterResult = new FilterResults();
filterResult.values = suggestions;
filterResult.count = suggestions.size();
return filterResult;
} else {
return new FilterResults();
}
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0) {
lstFollowing = (List<HashMap<String, String>>) results.values;
notifyDataSetChanged();
} else {
lstFollowing.clear();
lstFollowing.addAll(tempLstFollowing);
}
}
};
} catch (Exception e) {
e.printStackTrace();
}
}
}