Hello! This is my code:
private class NameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<Order> filt = new ArrayList<Order>();
ArrayList<Order> lItems = new ArrayList<Order>();
synchronized (this)
{
lItems.addAll(items);
}
for(int i = 0, l = lItems.size(); i < l; i++)
{
Order m = lItems.get(i);
if(m.getOrderTitle().toLowerCase().contains(constraint)
|| m.getOrderTime().toLowerCase().contains(constraint) ||
m.getOrderPrice().toLowerCase().contains(constraint))
filt.add(m);
}
result.count = filt.size();
result.values = filt;
}
else
{
synchronized(this)
{
result.values = items;
result.count = items.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered = (ArrayList<Order>)results.values;
adapter.notifyDataSetChanged();
adapter.clear();
if(!constraint.equals("")){
for(int i = 0; i < filtered.size(); i++){
adapter.add(filtered.get(i));
adapter.setId(i, filtered.get(i).getOrderId());
}
}
else {
if(orders!=null){
for(int i = 0; i < orders.size(); i++){
adapter.add(orders.get(i));
adapter.setId(i, orders.get(i).getOrderId());
}
}
}
}
}
The items in the ListView comes from an AsyncTask. When the AsyncTask is running I want it to stop adding non-matching items. For now, if it had loaded 5 items and I search with a specified String, then when the AsyncTask continues loading items it adds automatically Orders to the ListView, even if there isn't a match.
How can I fix this?
Thanks in advance and tell me if the question is unclear!