I have some problem with MultiAutoCompleteTextView, all seems to work correctly, writing on textedit I get the correct filtered values, the problem when I select a value, after click I get on textedit the wrong value, seems to be the first or second value of users instead filteredUser...where is the mistake?
public class AutocompleteUserAdapter extends ArrayAdapter {
private FriendOption selectedUser;
private final List<FriendOption> users;
private List<FriendOption> filteredUser = new ArrayList<>();
public AutocompleteUserAdapter(Context context, List<FriendOption> users) {
super(context, 0, users);
this.users = users;
}
@Override
public int getCount() {
return filteredUser.size();
}
public FriendOption getFilteredItemAtPosition(int pos){
return this.filteredUser.get(pos);
}
@Override
public Filter getFilter() {
return new UsersFilter(this, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FriendOption user = filteredUser.get(position);
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_user_simple, parent, false);
TextView textViewName = (TextView) convertView.findViewById(R.id.user_name);
textViewName.setText(user.getFriend().getName() + " " + user.getFriend().getSurname());
return convertView;
}
class UsersFilter extends Filter {
AutocompleteUserAdapter adapter;
List<FriendOption> originalList;
List<FriendOption> filteredList;
public UsersFilter(AutocompleteUserAdapter adapter, List<FriendOption> originalList) {
super();
this.adapter = adapter;
this.originalList = originalList;
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
filteredList.clear();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(originalList);
}
else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final FriendOption user: originalList) {
String name = user.getFriend().getName().toLowerCase();
if (name.contains(filterPattern)) {
filteredList.add(user);
}
}
}
final FilterResults results = new FilterResults();
results.values = filteredList;
results.count = filteredList.size();
return results;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null){
adapter.filteredUser.clear();
adapter.filteredUser.addAll((List) results.values);
for (FriendOption u:adapter.filteredUser){
System.out.println("user: " + u.getFriend().getName() + " " + u.getFriend().getSurname());
}
adapter.notifyDataSetChanged();
}
}
@Override
public String convertResultToString(Object resultValue) {
String result = ((FriendOption) resultValue).getFriend().getName() + " "+ ((FriendOption) resultValue).getFriend().getSurname();
return result;
}
}
find the problem, forgot
@Override
public Object getItem(int position) {
return filteredUser.get(position);
}