2011-09-16 47 views
1

我有兩個相互競爭的問題。使用AsyncTask進行Android ListView過濾

形勢

明白了我試圖通過對變化的EditText來篩選海量列表視圖10K +項目。

錯誤1

ANR keyDispatchingTimedOut error

我有UI線程(縫在合理的時間)的過濾操作,我認爲是什麼原因造成在某些手機上的錯誤.. 。

爲錯誤1

試圖修復創建一個AsyncTask特別對於C阿靈我的過濾功能...

/// <summary> 
    /// Implementation of Android AsyncTask to perform the StudentFiltering in background 
    /// </summary> 
    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); 
     } 
    } 

錯誤2

所以,現在,因爲我已經有了另一個線程採取過濾的照顧,它不告訴我,我不應該用後臺線程此操作類型,它應該只發生在UI線程上。

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} 

我錯過了一些東西,但是做了這些錯誤似乎有衝突嗎?你如何協調它們?

+0

您是否考慮讓XML解析器將數據插入SQLite數據庫,然後使用'CursorAdapter'將它綁定到您的'ListView'?這會給你更清潔的代碼,沒有線程問題和更好的性能。 –

回答

1

我認爲,問題是在這裏

protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params) 
    { 
     // filter the list 
     this._adapter.filterStudents(this._filterText); 
     return true; 
    } 

您無法從沒有UI線程存取權限Apapter類。嘗試做這樣的事情

protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params) 
    { 
     // filter the list 
     result = students.filterStudents(this._filterText); 
     return true; 
    } 


protected override void OnPostExecute(Java.Lang.Object result) 
     { 
      // Notify adapter of Data Change (to trigger re-draw) 
      this._adapter.SetItems(result); 
      this._adapter.NotifyDataSetChanged(); 
      base.OnPostExecute(result); 
     }