Firstly, I am really new in AutoCompleteTextView
, and as can be inferred from the title, I want to use it to Tag
people in the social media app I am currently developing.
First I've created a class named TagList
to contain a list of model class named TagModel
:
public class TagModel {
public String user_avatar;
public String user_name;
@Override
public String toString() {
return user_name;
}
}
And here's the whole adapter class :
public class TagAdapter extends ArrayAdapter<TagModel> implements Filterable {
private Context context;
private int resource;
private TagList mDataset;
public TagAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
this.resource = resource;
this.mDataset = new TagList();
}
public void changeItem(TagList tagList){
this.mDataset.tagModelList.clear();
notifyDataSetChanged();
this.mDataset.tagModelList.addAll(tagList.tagModelList);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mDataset.tagModelList.size();
}
@Override
public TagModel getItem(int position) {
return this.mDataset.tagModelList.get(position);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<TagModel> tempTagModel = new ArrayList<TagModel>();
if(constraint != null) {
int length = mDataset.tagModelList.size();
int i = 0;
while(i<length){
TagModel item = mDataset.tagModelList.get(i);
//do whatever you wanna do here
//adding result set output array
tempTagModel.add(item);
i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempTagModel;
filterResults.count = tempTagModel.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, Filter.FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try{
if(convertView==null){
// inflate the layout
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resource, parent, false);
}
TagModel objectItem = mDataset.tagModelList.get(position);
ImageView userAvatar = (ImageView) convertView.findViewById(R.id.popup_tag_avatar);
OpenSansFont userName = (OpenSansFont) convertView.findViewById(R.id.popup_tag_name);
userName.setText(objectItem.user_name);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
I've added single dummy data to test whether the AutoComplete
works, well, it works but now I'm at loss on filtering @(someone's name)
because whatever I type, it'll display my dummy data in the list.
I actually need 2 things done here,
Getting filter to filter only @(someone's name) and put the query into API.
Stop autocomplete when user presses space.
Can anyone point me to the right direction on this? I'll gladly put up any code needed to explain my question..
Update : A buggy TextWatcher on an AutoCompleteTextView
, trying to get whatever text after @
and before " "
.
tagAutoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 0) {
if (before == 0) {
flag = false;
stringBuilder.replace(0, stringBuilder.length() - 1, "");
}
if (flag) {
stringBuilder.append(s.toString().charAt(count - 1));
FriendTagListShowAPI friendTagListShowAPI = new FriendTagListShowAPI();
friendTagListShowAPI.query.tag = stringBuilder.toString();
friendTagListShowAPI.query.user_id = userId;
FriendTagListShowAPIFunc friendTagListShowAPIFunc = new FriendTagListShowAPIFunc();
friendTagListShowAPIFunc.delegate = TestAutoComplete.this;
friendTagListShowAPIFunc.execute(friendTagListShowAPI);
}
if (before < s.length() && String.valueOf(s.charAt(before)).equals("@")) {
flag = true;
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});