Android : Update MultiAutoCompleteTextView dropdown android

on Monday, September 15, 2014


I want to implement user tagging feature in my android application. I want same functionality like facebook user tag in comments. For that i implemented Custom MultiAutoCompleteTextView in my app and i also wanted to display both user image and user name. for that i implemented custom SimpleAdapter with custom filter for MultiAutoCompleteTextView. i referred this link and achieved this functionality.


I want to implement that, whenever i select any item from dropdown, that item should be removed from dropdown and dropdown data should be updated with remaining data. And if i erase that selected item explicitly from AutocompleteText then that erased item should re-appear in dropdown selection box. I don't know how to achieve this. I searched a lot but could get any proper solution. Please help me to solve this problem.


Thank you.


Code: (Activity)



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);


Commentadapter :



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();
}
}
}

0 comments:

Post a Comment