要实现AutoCompleteTextView与SimpleCursorAdapter不进行过滤,可以自定义一个Adapter来替代SimpleCursorAdapter,并重写getFilter()方法返回一个空的Filter对象。
以下是一个示例代码:
public class NoFilterAdapter extends SimpleCursorAdapter {
public NoFilterAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public Filter getFilter() {
return new NoFilter();
}
private class NoFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
Cursor cursor = getCursor();
if (cursor != null) {
results.values = cursor;
results.count = cursor.getCount();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// 不做任何过滤操作
notifyDataSetChanged();
}
}
}
然后在使用AutoCompleteTextView时,使用NoFilterAdapter替代SimpleCursorAdapter:
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
Cursor cursor = // 从数据库或其他地方获取数据的游标
String[] from = // 数据库中的字段名数组
int[] to = // 目标视图的id数组
NoFilterAdapter adapter = new NoFilterAdapter(this, android.R.layout.simple_dropdown_item_1line,
cursor, from, to, 0);
autoCompleteTextView.setAdapter(adapter);
这样就实现了AutoCompleteTextView与SimpleCursorAdapter不进行过滤的效果。