2016-01-28 15 views
5

Tôi muốn thay đổi chế độ lọc mặc định theo số AutoCompleteTextView. Bộ lọc mặc định tìm tất cả các chuỗi bắt đầu với mã thông báo đã cho. Dự án của tôi yêu cầu bộ lọc phải tìm tất cả các chuỗi chứa mã thông báo đã cho.Thay đổi bộ lọc AutoCompleteTextView từ "startsWith" thành "Contains"?

Có thể không?

+0

Xin vui lòng đọc câu trả lời của tôi tại http://stackoverflow.com/questions/32926034/autocompletetextview-not-completing-words-inside-parentheses/32928446#32928446 – BNK

+0

Hãy xem giải pháp này cũng: http: // stackoverflow. com/a/37298258/1808829 –

Trả lời

13

Tôi đã tìm thấy giải pháp cho điều đó, nhờ Google và tìm kiếm trong hai ngày. Như @ torque203 đã đề xuất, tôi đã triển khai Bộ điều hợp tùy chỉnh của riêng mình. Đầu tiên xác định một tập tin XML mới để tùy chỉnh Item trong bộ chuyển đổi:

autocomplete_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp" 
     android:id="@+id/lbl_name" /> 
</RelativeLayout> 

Tạo lớp mới cho Tên của bạn:

Tên

public class Names { 
    public String name; 
} 

NamesAdapter

public class NamesAdapter extends ArrayAdapter<Names> { 

    Context context; 
    int resource, textViewResourceId; 
    List<Names> items, tempItems, suggestions; 

    public NamesAdapter(Context context, int resource, int textViewResourceId, List<Names> items) { 
     super(context, resource, textViewResourceId, items); 
     this.context = context; 
     this.resource = resource; 
     this.textViewResourceId = textViewResourceId; 
     this.items = items; 
     tempItems = new ArrayList<Names>(items); // this makes the difference. 
     suggestions = new ArrayList<Names>(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.autocomplete_item, parent, false); 
     } 
     Names names = items.get(position); 
     if (names != null) { 
      TextView lblName = (TextView) view.findViewById(R.id.lbl_name); 
      if (lblName != null) 
       lblName.setText(names.name); 
     } 
     return view; 
    } 

    @Override 
    public Filter getFilter() { 
     return nameFilter; 
    } 

    /** 
    * Custom Filter implementation for custom suggestions we provide. 
    */ 
    Filter nameFilter = new Filter() { 
     @Override 
     public CharSequence convertResultToString(Object resultValue) { 
      String str = ((Names) resultValue).name; 
      return str; 
     } 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      if (constraint != null) { 
       suggestions.clear(); 
       for (Names names : tempItems) { 
        if (names.name.toLowerCase().contains(constraint.toString().toLowerCase())) { 
         suggestions.add(names); 
        } 
       } 
       FilterResults filterResults = new FilterResults(); 
       filterResults.values = suggestions; 
       filterResults.count = suggestions.size(); 
       return filterResults; 
      } else { 
       return new FilterResults(); 
      } 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      List<Names> filterList = (ArrayList<Names>) results.values; 
      if (results != null && results.count > 0) { 
       clear(); 
       for (Names names : filterList) { 
        add(names); 
        notifyDataSetChanged(); 
       } 
      } 
     } 
    }; 
} 

SearchActivity (hoặc hoạt động chính của bạn)

.... 
    List<Names> namesList = //your names list; 
    NamesAdapter namesAdapter = new NamesAdapter(
        SearchActivity.this, 
        R.layout.activity_search, 
        R.id.lbl_name, 
        namesList 
      ); 
      //set adapter into listStudent 
      autoCompleteTextView.setAdapter(namesAdapter); 
      autoCompleteTextView.showDropDown(); 
... 
+1

Điều này có vẻ rất hứa hẹn. –

+0

'' 'UnsupportedOperationException''' cho phương thức' '' clear''' trong '' 'publishResults'''. – JCarlos

+0

@JCarlos https: // stackoverflow.com/a/18685928/5222156 – madim

0

Có, có thể.

cách đầu tiên:

Bạn nên tạo một Adapter tùy chỉnh mà thực hiện ListAdapterFilterable.

Filter hơn có thể triển khai logic bộ lọc "chứa" của bạn.

Và bạn đặt bộ tiếp hợp này làm bộ điều hợp cho Chế độ xem tự động hoàn thành.

cách thứ hai:

Nếu bạn đã sử dụng ArrayAdapter. Bạn chỉ có thể ghi đè phương thức getFilter() của mình.

0

Chỉ cần để mở rộng câu trả lời tốt đẹp của Caffe Latte:

1) autoCompleteTextView.showDropDown(); không cần thiết.

2) Để lấy lại đối tượng đầu vào người ta có thể sử dụng:

//retrieve the input in the autoCompleteTextView 
     autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      //parent The AdapterView where the click happened. 
      //view The view within the AdapterView that was clicked (this will be a view provided by the adapter) 
      //position The position of the view in the adapter 
      //id The row id of the item that was clicked. 
      public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) { 
       String selection =parent.getItemAtPosition(position).toString(); 
       Toast.makeText(parent.getContext(),"" + selection,Toast.LENGTH_SHORT).show(); 
      } 
     }); 

Đối tượng lấy từ phụ huynh phải có một phương thức toString() thực hiện.

Các vấn đề liên quan