这是AutoCompleteAdapter
我在我的一个应用程序中使用的。我希望这能解决你的问题
将适配器从下方设置为您的AutoCompleteTextView
控件:
ArrayAdapter<String> adapter = new AutoCompleteAdapter(this,
R.layout.dropdown_item);
autoComplete.setAdapter(adapter);
样品适配器:
private class AutoCompleteAdapter extends ArrayAdapter<String>
implements Filterable {
private ArrayList<String> mData;
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mData = new ArrayList<String>();
}
@Override
public int getCount() {
try {
return mData.size();
} catch (NullPointerException e) {
return 0;
}
}
@Override
public String getItem(int index) {
return mData.get(index);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
//This shows a progress to the user in my app. you don't need to use this
handle.sendEmptyMessage(MSG_SHOW_PROGRESS);
try {
//Fill mData with your data
mData = XmlParser.searchLocations(constraint
.toString());
} catch (Exception e) {
handle.sendEmptyMessage(MSG_HIDE_PROGRESS);
}
mData.add("NOT IN LIST");
filterResults.values = mData;
filterResults.count = mData.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
handle.sendEmptyMessage(MSG_HIDE_PROGRESS);
} else {
notifyDataSetInvalidated();
handle.sendEmptyMessage(MSG_HIDE_PROGRESS);
}
}
};
return myFilter;
}
}