如何将自定义适配器添加到AutoCompleteTextView

有没有简单的方法来设置一个2 TextView下拉到一个AutoCompleteTextView。

android.R.layout.two_line_list_item我找不到任何例子如何使用。

所以,我试过这个:

 public class TwoLineDropdownAdapter extends BaseAdapter { private LayoutInflater mInflater = null; private Activity activity; public ArrayList<TwoLineDropDown> values = new ArrayList<TwoLineDropDown>(); public TwoLineDropdownAdapter(Activity a, ArrayList<TwoLineDropDown> items) { values = items; activity = a; mInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { return values.size(); } public TwoLineDropDown getItem(int position) { return values.get(position); } public long getItemId(int position) { return position; } public static class ViewHolder { public TextView title; public TextView description; } public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.dropdown_text_twoline, parent, false); holder.title = (TextView) convertView .findViewById(R.id.text1); holder.description = (TextView) convertView .findViewById(R.id.text2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } return convertView; } public void add(TwoLineDropDown ei) { values.add(ei); } } 

但是我在这里面临一个问题:

 TwoLineDropdownAdapter AutoCompleteAdapter = new TwoLineDropdownAdapter(this, items); myAutoComplete.setAdapter(AutoCompleteAdapter); 

同时设置适配器它说:

绑定不匹配:AutoCompleteTextViewtypes的generics方法setAdapter(T)不适用于参数(TwoLineDropdownAdapter)。 推断的typesTwoLineDropdownAdapter不是绑定参数的有效替代

如何解决这个问题?

谢谢

Related of "如何将自定义适配器添加到AutoCompleteTextView"

根据文档,AutoCompleteTextView中的setAdapter的推断types是:

 <T extends ListAdapter & Filterable> void setAdapter(T adapter) 

你的适配器必须是一个ListAdapter(BaseAdapter,到目前为止这么好)和一个Filterable,这个BaseAdapter不是,也不是你的Adapter实现。 我会扩展一个ArrayAdapter,这是可过滤的,更不用说会简化你的实现(你的一些方法为同样的结果重复ArrayAdapter的方法):

 public class TwoLineDropdownAdapter extends ArrayAdapter<TwoLineDropDown> { private LayoutInflater mInflater = null; private Activity activity; public TwoLineDropdownAdapter(Activity a, ArrayList<TwoLineDropDown> items) { super(a, 0, items); activity = a; mInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public static class ViewHolder { public TextView title; public TextView description; } public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.dropdown_text_twoline, parent, false); holder.title = (TextView) convertView .findViewById(R.id.text1); holder.description = (TextView) convertView .findViewById(R.id.text2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } return convertView; } } 

这里Code正在为我工​​作,

将此适配器设置为autocompletetextview

 AutoCompleteTextView etProductSearch = (AutoCompleteTextView)getView().findViewById(R.id.edtSearchBoxTakeOrder); ProductSearchAdapter adapter = new ProductSearchAdapter(getActivity(), android.R.layout.simple_dropdown_item_1line, productList); etProductSearch.setAdapter(adapter ); 

ProductSearchAdapter类

 import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Filter; import android.widget.TextView; public class ProductSearchAdapter extends ArrayAdapter<ProductDataModel> { private ArrayList<ProductDataModel> items; private ArrayList<ProductDataModel> itemsAll; private ArrayList<ProductDataModel> suggestions; private int viewResourceId; @SuppressWarnings("unchecked") public ProductSearchAdapter(Context context, int viewResourceId, ArrayList<ProductDataModel> items) { super(context, viewResourceId, items); this.items = items; this.itemsAll = (ArrayList<ProductDataModel>) items.clone(); this.suggestions = new ArrayList<ProductDataModel>(); this.viewResourceId = viewResourceId; } public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(viewResourceId, null); } ProductDataModel product = items.get(position); if (product != null) { TextView productLabel = (TextView) v.findViewById(android.R.id.text1); if (productLabel != null) { productLabel.setText(product.getProductName()); } } return v; } @Override public Filter getFilter() { return nameFilter; } Filter nameFilter = new Filter() { public String convertResultToString(Object resultValue) { String str = ((ProductDataModel) (resultValue)).getProductName(); return str; } @Override protected FilterResults performFiltering(CharSequence constraint) { if (constraint != null) { suggestions.clear(); for (ProductDataModel product : itemsAll) { if (product.getProductName().toLowerCase() .startsWith(constraint.toString().toLowerCase())) { suggestions.add(product); } } 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) { @SuppressWarnings("unchecked") ArrayList<ProductDataModel> filteredList = (ArrayList<ProductDataModel>) results.values; if (results != null && results.count > 0) { clear(); for (ProductDataModel c : filteredList) { add(c); } notifyDataSetChanged(); } } }; } 

我相信最简单的方法就是扩展SimpleAdapter

 public class MyAdapter extends android.widget.SimpleAdapter { static ArrayList<Map<String, String>> toMapList(Collection<MyObject> objectsCollection) { ArrayList<Map<String, String>> objectsList = new ArrayList<Map<String, String>>(objectsCollection.size()); for (MyObject obj : objectsCollection) { Map<String, String> map = new HashMap<String, String>(); map.put("name", obj.getName()); map.put("details", obj.getDetails()); objectsList.add(map); }; return objectsList; } public MyAdapter(Context context, Collection<MyObject> objects) { super(context, toMapList(objects), R.layout.auto_complete_layout, new String[] {"name", "description"}, new int[] {R.id.name, R.id.description}); } } 

主要的缺点是这将使候选人以任何名称或描述中的任何以空格分隔的词语为基础。 如果向auto_complete_layout添加另一个字段,它也将涉及匹配。

因此,我完成了重写SimpleAdapter以更好地适应我的需求,消除了大量与我的用例无关的基类开销。 但是,上面的几行给你一个好的开始,并提供一个可靠的参考来开始定制。