getFilter()
是的,您可以通过实现 ArrayAdapter 来做到这一点,但您需要实现AutoCompleteTextView
. 与此类似的东西可能会起作用:
private class FirmAdapter extends ArrayAdapter<Firm> {
private Filter filter;
@Override
public Filter getFilter()
{
if(filter == null){
filter = new Filter(){
int lastlen = 0;
boolean initialized = false;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
filteredItems = (List<Firm>)results.values;
ArrayAdapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
if(constraint == null || constraint.length() == 0){
results.values = null;
}else{
String val;
Firm obj;
constraint = constraint.toString().toUpperCase();
int newlen = constraint.length();
LinkedList<Firm> filteredResults = new LinkedList<Firm>();
if(newlen < lastlen){
int i = 0, size = ArrayAdapter.super.getCount();
for(i=0;i<size;i++){
obj = ArrayAdapter.super.getItem(i);
val = obj.toString(); // CUSTOMIZE THIS
if(val.contains(constraint)) filteredResults.add(obj);
}
}else{
int i = 0, size = getCount();
for(i=0;i<size;i++){
obj = getItem(i);
val = obj.toString(); // CUSTOMIZE THIS
if(val.contains(constraint)) filteredResults.add(obj);
}
}
lastlen = newlen;
results.values = filteredResults;
}
return results;
}
};
}
return filter;
}
}
}
我从我的一个项目中删除了这个,所以它需要一些测试和清理才能开始工作(在我的情况下,我在一组JSONObject
实例上有一个过滤器,你有一个Firm
)但是尝试类似的东西。带有的注释CUSTOMIZE THIS
是您希望根据输入到AutoCompleteTextView
.