我正在尝试为自定义列表视图创建过滤器,过滤器的结果将继续显示arraylist中的所有项目。在检查了日志之后,我发现filteredList中有itme,但是results.count返回0,这是从“运行”窗口中键入“ a”时的日志: V /清单:沙拉 V /值:0
@Override
public Filter getFilter(){
return customFilter;
}
private Filter customFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Favorite> filteredList = new ArrayList<>();
if(constraint == null || constraint.length() == 0){
//filteredList.addAll(arrayList);
Toast.makeText(getContext(),"NOTHING FOUND",Toast.LENGTH_LONG);
}else {
String filterPattern = constraint.toString().toLowerCase().trim();
for(Favorite item: arrayList){
if (item.getTitle().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
Log.v("list",filteredList.get(0).getTitle());
FilterResults results = new FilterResults();
results.values = filteredList;
Log.v("value",Integer.toString(results.count));
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arrayList2.clear();
arrayList2.addAll((ArrayList<Favorite>) results.values);
notifyDataSetChanged();
}
};
我称之为过滤器的地方
searchTXT.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
FavoriteFragment.this.arrayAdapter.getFilter().filter(s);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
//filter(s.toString());
}
});`