title: Android ListView Filtering using AsyncTask
body: <p>I've got two competing problems.</p>
  
  <p><strong>Situation</strong></p>
  
  <p>Got a Massive Listview 10k+ items that I'm trying to filter via an <code>EditText</code> on change.</p>
  
  <p><strong>Error 1</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/5513457/anr-keydispatchingtimedout-error">ANR keyDispatchingTimedOut error</a></p>
  
  <p>I had the filtering operation on the UI thread (which seamed reasonable at the time), and I assume that is what is causing the error on some phones...</p>
  
  <p><strong>Attempted fix for Error 1</strong></p>
  
  <p>Created an <code>AsyncTask</code> specifically for calling my filter function...</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Implementation of Android AsyncTask to perform the StudentFiltering in background
      /// &lt;/summary&gt;
      internal class FilterStudentsTask : AsyncTask
      {
          private Student[] _students;
          private StudentList _outer;
          private StudentListAdapter _adapter;
          private string _filterText;
  
          public FilterStudentsTask(StudentList outer, StudentListAdapter adapter, string filterText)
          {
              this._outer = outer;
              this._adapter = adapter;
              this._filterText = filterText;
          }
  
          protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
          {
              // filter the list
              this._adapter.filterStudents(this._filterText);
              return true;
          }
  
          protected override void OnPostExecute(Java.Lang.Object result)
          {
              // Notify adapter of Data Change (to trigger re-draw)
              this._adapter.NotifyDataSetChanged();
              base.OnPostExecute(result);
          }
      }
  </code></pre>
  
  <p><strong>Error 2</strong></p>
  
  <p>So now, because I've got another thread taking care of the filtering, it's not telling me I should not use background threads for this type of operation, and it should only happen on the UI thread..????</p>
  
  <pre><code>E/AndroidRuntime( 9738): FATAL EXCEPTION: main
  E/AndroidRuntime( 9738): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sur
  e the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131361885, class mapdroid.ColorFade
  ListView) with Adapter(class mapdroid.StudentList_StudentListAdapter)]
  E/AndroidRuntime( 9738):        at android.widget.ListView.layoutChildren(ListView.java:1510)
  E/AndroidRuntime( 9738):        at android.widget.AbsListView.onLayout(AbsListView.java:1260)
  E/AndroidRuntime( 9738):        at android.view.View.layout(View.java:7175)
  E/AndroidRuntime( 9738):        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
  E/AndroidRuntime( 9738):        at android.view.View.layout(View.java:7175)
  E/AndroidRuntime( 9738):        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
  E/AndroidRuntime( 9738):        at android.view.View.layout(View.java:7175)
  E/AndroidRuntime( 9738):        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
  E/AndroidRuntime( 9738):        at android.view.View.layout(View.java:7175)
  E/AndroidRuntime( 9738):        at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
  E/AndroidRuntime( 9738):        at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
  E/AndroidRuntime( 9738):        at android.os.Handler.dispatchMessage(Handler.java:99)
  E/AndroidRuntime( 9738):        at android.os.Looper.loop(Looper.java:130)
  E/AndroidRuntime( 9738):        at android.app.ActivityThread.main(ActivityThread.java:3683)
  E/AndroidRuntime( 9738):        at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime( 9738):        at java.lang.reflect.Method.invoke(Method.java:507)
  E/AndroidRuntime( 9738):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  E/AndroidRuntime( 9738):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  E/AndroidRuntime( 9738):        at dalvik.system.NativeStart.main(Native Method)
  W/ActivityManager(  111):   Force finishing activity MapDroid.MapDroid/mapdroid.StudentList
  W/ActivityManager(  111): Activity pause timeout for HistoryRecord{40670aa0 MapDroid.MapDroid/mapdroid.StudentList}
  </code></pre>
  
  <p>Am I missing something but do these Errors seem to be in conflict? how do you reconcile them?</p>