2016-11-26 87 views
-2

我有一些MultiAutoCompleteTextView的問題,似乎工作正常,寫在textedit我得到正確的過濾值,當我選擇一個值,單擊我得到textedit錯誤的值後,問題似乎成爲用戶的第一或第二個值,而不是被過濾掉的用戶...錯誤在哪裏?MultiAutoCompleteTextView顯示錯誤的結果

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

回答

0

發現問題,忘記

@Override 
public Object getItem(int position) { 

    return filteredUser.get(position); 
}