有没有简单的方法来设定的2的TextView下拉到AutoCompleteTextView。
Is there any simple way to set a 2 TextView dropdown to an AutoCompleteTextView.
有 android.R.layout.two_line_list_item
这我找不到任何例子如何使用。
There is android.R.layout.two_line_list_item
Which I couldn't find any examples how to use.
所以,我想这样的:
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);
在设置适配器,它说:
约束不匹配:通用方法setAdapter型(T) AutoCompleteTextView是不适用的参数 (TwoLineDropdownAdapter)。推断的类型TwoLineDropdownAdapter是 为界参数不是一个有效的替代品
Bound mismatch: The generic method setAdapter(T) of type AutoCompleteTextView is not applicable for the arguments (TwoLineDropdownAdapter). The inferred type TwoLineDropdownAdapter is not a valid substitute for the bounded parameter
如何解决此问题?
感谢您
根据该文件,推断的类型setAdapter在AutoCompleteTextView是:
According to the documentation, the inferred type of setAdapter in AutoCompleteTextView is :
<T extends ListAdapter & Filterable> void setAdapter(T adapter)
您适配器必须是ListAdapter(这BaseAdapter是,到目前为止一切顺利)和过滤的,这BaseAdapter不是,也不是你的适配器实现。我将延长一个ArrayAdapter,这是过滤的,更何况是将简化你的实现(你的一些方法复制ArrayAdapter的相同结果的方法):
Your adapter must be a ListAdapter (which BaseAdapter is, so far so good) and a Filterable, which BaseAdapter is not, nor is your Adapter implementation. I would extend an ArrayAdapter, which is Filterable, not to mention is would simplify your implementation (some of your methods duplicate methods of ArrayAdapter for the same result) :
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;
}
}