MultiAutoCompleteTextView 显示错误的结果
MultiAutoCompleteTextView show wrong result
我在使用 MultiAutoCompleteTextView 时遇到了一些问题,一切似乎都正常工作,在 textedit 上写我得到了正确的过滤值,当我 select 一个值时出现问题,点击后我在 textedit 上得到了错误的值,似乎是用户的第一个或第二个值而不是filteredUser...错误在哪里?
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;
}
}
发现问题,忘了
@Override
public Object getItem(int position) {
return filteredUser.get(position);
}
我在使用 MultiAutoCompleteTextView 时遇到了一些问题,一切似乎都正常工作,在 textedit 上写我得到了正确的过滤值,当我 select 一个值时出现问题,点击后我在 textedit 上得到了错误的值,似乎是用户的第一个或第二个值而不是filteredUser...错误在哪里?
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;
}
}
发现问题,忘了
@Override
public Object getItem(int position) {
return filteredUser.get(position);
}