2015-06-24 101 views
0

我已經做了一些Google-fu,但我無法弄清楚什麼是錯的。過濾工作,出現下拉列表。但AutoCompleteTextView不會填充選定的項目!誰能幫忙?AutoCompleteTextView沒有填充選定的項目

我爲我的AutoCompleteTextView設置了一個顯示自定義佈局的自定義適配器。

actv = (AutoCompleteTextView) root.findViewById(R.id.actv); 
actv.setAdapter(new MyCustomAutoCompleteAdapter(getActivity())); 

這裏是重要的部分MyCustomAutoCompleteAdapter代碼:

public class MyCustomAutoCompleteAdapter extends ArrayAdapter<String> 
    implements Filterable { 

    Context mContext; 

    private ArrayList<String> resultList = new ArrayList<>(); 

    public MyCustomAutoCompleteAdapter(Context context) { 
    // is this the correct way to super? 
    super(context, R.layout.my_custom_layout); 
    mContext = context; 


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

    @Override 
    public int getCount() { 
     return resultList.size(); 
    } 

    @Override 
    public String getItem(int index) { 
     return resultList.get(index); 
    } 

    @Override 
    public Filter getFilter() { 
     Filter filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults filterResults = new FilterResults(); 
       if (constraint != null) { 
        // Retrieve the autocomplete results. 
        resultList = doPlacesSearchQuery(constraint.toString()); 

        // Assign the data to the FilterResults 
        filterResults.values = resultList; 
        filterResults.count = resultList.size(); 
       } 
       return filterResults; 
      } 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       if (results != null && results.count > 0) { 
        notifyDataSetChanged(); 
       } 
       else { 
        notifyDataSetInvalidated(); 
       } 
      }}; 
     return filter; 
    } 

    private ArrayList<String> doPlacesSearchQuery(String query) { 

     ArrayList<String> retList = new ArrayList<>(); 

     ... // do my API call here 

     return retList; 
    } 
} 
+0

看到我的答案在這裏http://stackoverflow.com/a/19860624/2252830 – pskink

回答

0

我發現我的答案;我的確在使用錯誤的超級構造函數。

public MyCustomAutoCompleteAdapter(Context context) { 
    super(context, 
      R.layout.my_custom_layout, 
      R.id.id_of_textview_in_my_custom_layout); 
    mContext = context; 
} 
相關問題