我有一个带有自定义适配器的listView。 效果很好,但现在我尝试向其添加自定义过滤器。 过滤器的逻辑运作良好,我在日志中可以看到过滤器找到了正确的对象。但是由于某种原因,它不会更新视图。 这是我的代码:
ListView:
Collections.sort(events);
Log.d("2", events.toString());
final EventsLayoutAdapter mAdapter = new EventsLayoutAdapter(getApplicationContext(), events);
lvEvents.setAdapter(mAdapter);
svEvent.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
mAdapter.getFilter().filter(query);
lvEvents.setAdapter(mAdapter);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// adapter.getFilter().filter(newText);
return false;
}
});
这是适配器的过滤器功能,getView和contractor不在这里:
public class EventsLayoutAdapter extends ArrayAdapter<ServerEvent> implements Filterable {
private ArrayList<ServerEvent> dataSet;
Context mContext;
LocalFilter mFilter;
@Override
public Filter getFilter() {
if (mFilter == null)
mFilter = new LocalFilter();
return mFilter;
}
private class LocalFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
Log.d("2","Filter logic began");
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = dataSet;
results.count = dataSet.size();
Log.d("2","Filter logic no filter: "+ dataSet.size());
} else {
// We perform filtering operation
Log.d("2","Filter logic filter operation : "+ dataSet.size());
List<ServerEvent> nArrayList = new ArrayList<ServerEvent>();
for (ServerEvent item : dataSet) {
if(item.getTitle().toUpperCase().contains(constraint.toString().toUpperCase())||
item.getPlaceName().toUpperCase().contains(constraint.toString().toUpperCase())||
(item.isPriority()?"Important Event":"Regular Event").toUpperCase().contains(constraint.toString().toUpperCase())||
BroardFunctions.unixToDate(item.getUnixTime()).contains(constraint.toString())){
nArrayList.add(item);
Log.d("2",nArrayList.toString());
}
}
results.values = nArrayList;
results.count = nArrayList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Now we have to inform the adapter about the new list filtered
dataSet = (ArrayList<ServerEvent>) results.values;
notifyDataSetChanged();
}
}
}
它会在过滤后返回正确的结果,但列表视图不会在最后更新。