views:

39

answers:

1

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!

A: 

According to your earlier question(s) here on SO, you can check in onProgressUpdate if your EditText is empty or not. If it contains some text, then filter it at that point.

@Override
protected void onProgressUpdate(Integer... i) { 

   CharSequence cs = yourEditText.getText().toString(); //Get text
   if(!cs.equals("")){ //Check if "cs" contains text
      adapter.add(orders.get(i[0])); //Add it to the adapter, then...    
      adapter.getFilter().filter(cs); //..do your filtering, and...
      adapter.notifyDataSetChanged(); //...update your View
   }
   else { //Otherwise, just add the order to your adapter..
       adapter.add(orders.get(i[0])); 
   }

}

We always add all orders (even if the EditText contains information) to make sure that all orders show up when EditText is empty.

Charlie Sheen
Thanks! That was useful!
Julian Assange