现在我有一个AutoCompleteTextView,我希望它像like "%xxx%"
一样工作
在SQL中。我尝试使用Filterable来实现它我已经拥有它并且代码运行但它现在只显示所有内容,即使没有部分匹配。任何帮助将不胜感激。
public class CodesArrayAdapter extends ArrayAdapter implements Filterable{
List<String> allCodes;
List<String> originalCodes;
StringFilter filter;
public CodesArrayAdapter(Context context, int resource, List<String> keys) {
super(context, resource, keys);
allCodes=keys;
originalCodes=keys;
}
public int getCount() {
return allCodes.size();
}
public Object getItem(int position) {
return allCodes.get(position);
}
public long getItemId(int position) {
return position;
}
private class StringFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalCodes;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
allCodes = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
如果这是适配器的完整代码,您就错过了实施getFilter()
方法,即
@Override
public Filter getFilter()
{
return new StringFilter();
}