title: Android Correct way if implementing filterable in Auto-Complete
body: <p>What i am trying to do is and failing miserably is this:</p>
  
  <p>In my view I have an auto-complete Textview.
  I would like to fetch via a GET a json array of objects. Takes about 1-2 secs...
  <em>(Should  i use AsyncTask or a Handler to support this fetch?)</em></p>
  
  <p>Then filter user's input based on this array.</p>
  
  <p>Currently I have implemented my custom adaptor as so...</p>
  
  <pre><code>public class StationAdapter extends BaseAdapter implements Filterable {
  
  Context _ctx;
  //returned stations...
  ArrayList&lt;Station&gt; _stations;
  
  // to hold original stations...
  private ArrayList&lt;Station&gt; orig;
  
  //Custom filter to be used
  private final StationFilter filter;
  
  public StationAdapter(final Context ctx, final ArrayList&lt;Station&gt; stations) {
      this._ctx = ctx;
      this._stations = stations;
      this.orig = stations;
      this.filter = new StationFilter();
  }
  
  @Override
  public int getCount() {
      if (_stations != null)
          return _stations.size();
      else
          return 0;
  }
  
  @Override
  public Object getItem(final int position) {
      return _stations.get(position);
  }
  
  
  //IS unused? NO whats its real purpose ?...
  @Override
  public long getItemId(final int position) {
      return (position);
  }
  
  
  @Override
  public View getView(final int position, final View convertView, final ViewGroup parent) {
      StationView sv;
      if (convertView == null)
          sv = new StationView(_ctx, _stations.get(position));
      else {
          sv = (StationView) convertView;
          sv.setCode(_stations.get(position).mCode);
          sv.setName(_stations.get(position).mName);
      }
      return sv;
  }
  
  @Override
  public Filter getFilter() {
      return filter;
  }
  
  private class StationFilter extends Filter {
  
      @Override
      protected FilterResults performFiltering(final CharSequence constraint) {
  
          final FilterResults oReturn = new FilterResults();
          final ArrayList&lt;Station&gt; results = new ArrayList&lt;Station&gt;();
          if (orig == null)
              orig = _stations;
          if (constraint != null) {
              if (orig != null &amp;&amp; orig.size() &gt; 0) {
                  for (final Station g : orig) {
                      if (g.mName.contains(constraint.toString().toUpperCase()))
                          results.add(g);
                  }
              }
              oReturn.values = results;
          }
          return oReturn;
  
      }
  
      @SuppressWarnings("unchecked")
      @Override
      protected void publishResults(final CharSequence constraint, final FilterResults results) {
          _stations = (ArrayList&lt;Station&gt;) results.values;
          notifyDataSetChanged();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>For some reason the filtered response is triggered every other input key my main.xml looks simply like this...</p>
  
  <pre><code>        &lt;AutoCompleteTextView android:id="@+id/search_stations"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:lines="1"
          android:singleLine="true"          
          /&gt;
  </code></pre>
  
  <p>Could someone point out what i am doing wrong and some tutorials that deal with such a use-case</p>
  
  <p>Thanks in advance</p>