我使用java class Pattern过滤搜索query
。我希望返回的项目是字母数字顺序 - 即A-Z0-9,不区分大小写。我如何在下面的代码中实现它?是的它是Android,但它都是Java :)过滤器有效,我只需按顺序放置pattern
结果!提前谢谢。
这里定义了过滤器:
@Override
public android.widget.Filter getFilter() {
return new Filter(
new AnonListAdapter.Predicate<InstanceAnonChartData, CharSequence>() {
public boolean test(InstanceAnonChartData instance,
CharSequence constraint) {
if (constraint != null) {
String query = constraint.toString();
if (query.isEmpty())
return true;
// Matches any word starting with the given string
Pattern pattern = Pattern.compile("(?i-m)(^|\\W)" + query);
// Check if the pattern matches either the symbol or the name
return pattern.matcher(instance.getName()).find() ||
pattern.matcher(instance.getSymbol()).find();
}
return true;
}
});
}
这里实现了过滤器:
public void applyFilter(CharSequence query) {
if (_listAdapter != null && query != null) {
Filter listFilter = _listAdapter.getFilter();
if (listFilter != null) {
listFilter.filter(query);
}
}
}
...
private void handleIntent(@NonNull Intent intent) {
// Handle "Search" intents
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// SearchManager.QUERY is the key that a SearchManager will use to send a query string
// to an Activity.
String query = intent.getStringExtra(SearchManager.QUERY);
_listFragment.applyFilter(query);
}
}