title: Android How do I overwrite the filter for my ArrayAdapter?
body: <p>I am trying to write a custom filter to filter the arraylist in my arrayadapter such that my listview is filtered when I click on the button.</p>
  <p>For instance when I click on my button</p>
  <pre><code>public void onClick(View arg0) {
              String abc = &quot;abc&quot;;
              m_adapter.getFilter().filter(abc);
          }
  </code></pre>
  <p>However, when I click on my button, my app terminate unexpectedly. Here is my code for the arrayadapter and filter. Please help me.</p>
  <pre><code>package com.ntu.rosemobile.searchlist;
  
  public class ResultsAdapter extends ArrayAdapter&lt;SearchItem&gt; implements Filterable{
  
  public ArrayList&lt;SearchItem&gt; subItems;
  public ArrayList&lt;SearchItem&gt; allItems;
  private LayoutInflater inflater;
  private PTypeFilter filter;
  
  public ResultsAdapter(Context context, int textViewResourceId, ArrayList&lt;SearchItem&gt; items) {
          
      super(context, textViewResourceId, items);
          this.subItems = items;
          this.allItems = this.subItems;
          inflater= LayoutInflater.from(context);
  }
  
  @Override
  public Filter getFilter() {
      if (filter == null){
        filter  = new PTypeFilter();
      }
      return filter;
    }
  
  
  
  //@Override
  public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
          if (v == null) {
              
              v = inflater.inflate(R.layout.listrow, null);
          }
          SearchItem o = subItems.get(position);
          if (o != null) {
                  TextView pname = (TextView) v.findViewById(R.id.productname);
                  TextView neg = (TextView) v.findViewById(R.id.negNum);
                  TextView pos = (TextView) v.findViewById(R.id.posNum);
                  TextView neu = (TextView) v.findViewById(R.id.neuNum);
                  
                  WebImageView productPhoto = (WebImageView)v.findViewById(R.id.pPhoto);
                  if(productPhoto!=null){
                      productPhoto.setImageUrl(o.getImageUrl().toString());
                      productPhoto.loadImage();
                  }
                  if(pname!= null){
                      pname.setText(o.getProductName().toString());
                  }                    
                  if (neg != null) {
                        String a =  &quot;&quot; + o.getNegativeReviews();
                        neg.setText(a);                            
                  }
                  if(neu != null){
                       String a =  &quot;&quot; + o.getNeutralReviews();
                       neu.setText(a);
                  }
                  if(pos != null){
                      String a =  &quot;&quot; + o.getPositiveReviews();
                      pos.setText(a);
                  }
          }
          return v;
  }
  
  private class PTypeFilter extends Filter{
  
      
      @SuppressWarnings(&quot;unchecked&quot;)
      @Override
      protected void publishResults(CharSequence prefix,
                                    FilterResults results) {
        // NOTE: this function is *always* called from the UI thread.
         subItems =  (ArrayList&lt;SearchItem&gt;)results.values;
          
          notifyDataSetChanged();
      }
      
      @SuppressWarnings(&quot;unchecked&quot;)
      protected FilterResults performFiltering(CharSequence prefix) {
            // NOTE: this function is *always* called from a background thread, and
            // not the UI thread. 
            
            FilterResults results = new FilterResults();
            ArrayList&lt;SearchItem&gt; i = new ArrayList&lt;SearchItem&gt;();
            
            if (prefix!= null &amp;&amp; prefix.toString().length() &gt; 0) {
  
                for (int index = 0; index &lt; allItems.size(); index++) {
                    SearchItem si = allItems.get(index);
                    if(si.getPType().compareTo(prefix.toString()) == 0){
                      i.add(si);  
                    }
                }
                results.values = i;
                results.count = i.size();                   
            }
            else{
                synchronized (allItems){
                    results.values = allItems;
                    results.count = allItems.size();
                }
            }
            
            return results;
      }
    }     
  }
  </code></pre>