-1

我对 MultiAutoCompleteTextView 有一些问题,一切似乎都正常工作,在 textedit 上写入我得到正确的过滤值,当我选择一个值时出现问题,点击后我在 textedit 上得到错误的值,似乎是第一个或第二个值users 而是过滤了User ...错误在哪里?

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;
        }
    }
4

1 回答 1

1

发现问题,忘记了

@Override
public Object getItem(int position) {

    return filteredUser.get(position);
}
于 2016-11-26T14:12:16.623 回答