[英]Android ListView Filtering using AsyncTask
我有兩個相互競爭的問題。
情況
在嘗試更改時,我嘗試通過EditText
篩選出大量的Listview 10k +項目。
錯誤1
我在UI線程上進行了過濾操作(在當時看來是合理的),並且我認為這是導致某些手機出現錯誤的原因...
嘗試修復錯誤1
創建了一個AsyncTask
專門用於調用我的過濾器功能...
/// <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}
我是否缺少某些內容,但是這些錯誤似乎存在沖突嗎? 您如何調和他們?
我認為這個問題在這里
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.