0

這裏是:我輸入「di」,但AutoCompleteTextView顯示我無關的東西。AutoCompleteTextView顯示錯誤的結果

enter image description here

以下是我Init這:

autocompleteAdapter = new AutoCompleteDropdownAdapter(MainActivity.activity, 
android.R.layout.simple_list_item_1, autoCompleteStrings); 
etSearch.setAdapter(autocompleteAdapter); 

這是我的適配器(我試過標準ArrayAdapter,但結果是一樣的)

class AutoCompleteDropdownAdapter extends ArrayAdapter<String> implements Filterable { 

    Activity main; 
    int rowLayout; 
    List<String> strings; 

    private ArrayList<String> originalValues; 
    private ArrayFilter filter; 

    public AutoCompleteDropdownAdapter(Activity main, int rowLayout, List<String> strings) { 
     super(main, rowLayout, strings); 
     this.main = main; 
     this.rowLayout = rowLayout; 
     this.strings = strings; 
     originalValues = new ArrayList<String>(strings); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return super.getView(position, convertView, parent); 
    } 

    @Override 
    public Filter getFilter() { 
     if (filter == null) { 
      filter = new ArrayFilter(); 
     } 
     return filter; 
    } 

    private class ArrayFilter extends Filter { 
     private Object lock; 

     @Override 
     protected FilterResults performFiltering(CharSequence prefix) { 
      FilterResults results = new FilterResults(); 

      if (originalValues == null) { 
       synchronized (lock) { 
        originalValues = new ArrayList<String>(strings); 
       } 
      } 

      if (prefix == null || prefix.length() == 0) { 
       synchronized (lock) { 
        ArrayList<String> list = new ArrayList<String>(originalValues); 
        results.values = list; 
        results.count = list.size(); 
       } 
      } else { 
       final String prefixString = prefix.toString().toLowerCase(); 

       ArrayList<String> values = originalValues; 
       int count = values.size(); 

       ArrayList<String> newValues = new ArrayList<String>(count); 

       for (int i = 0; i < count; i++) { 
        String item = values.get(i); 
        if (item.toLowerCase().contains(prefixString)) { 
         newValues.add(item); 
        } 

       } 

       results.values = newValues; 
       results.count = newValues.size(); 
      } 

      return results; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 

     if(results.values!=null){ 
      ArrayList<String> tempStrings = (ArrayList<String>) results.values; 
      strings.clear(); 
      strings.addAll(tempStrings); 
     }else{ 
      strings = new ArrayList<String>(); 
     } 
      if (results.count > 0) { 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
     } 
    } 

現在,當我調試publishResults方法它顯示我正好需要2個結果,但不過,AutoCompleteTextView下降錯誤列表

+0

做添加適配器的所有代碼,如果你跳過一些 – Rasel

+0

沒有,這是?只是super.getView() – user2976267

回答

0

我認爲你可以從這個例子中找到一些線索:您使用吹氣並設置文本的下拉項

https://github.com/gabu/gabubon2/blob/master/src/net/gabuchan/androidrecipe/recipe033/Recipe033Activity.java#L14

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_recipe_033); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    AutoCompleteTextView v = (AutoCompleteTextView) 
      findViewById(R.id.auto_complete_text_view); 
    v.setAdapter(adapter); 
} 

public void onButtonClick(View view) { 
    AutoCompleteTextView v = (AutoCompleteTextView) 
      findViewById(R.id.auto_complete_text_view); 
    Toast.makeText(this, v.getText(), Toast.LENGTH_SHORT).show(); 
}