【问题标题】:Android ListView Filtering using AsyncTaskAndroid ListView 过滤使用 AsyncTask
【发布时间】:2011-09-16 15:34:35
【问题描述】:

我有两个相互竞争的问题。

情况

有一个海量的 Listview 10k+ 项目,我正试图通过 EditText 过滤更改。

错误 1

ANR keyDispatchingTimedOut error

我在 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}

我是否遗漏了什么,但这些错误似乎有冲突?你如何调和它们?

【问题讨论】:

  • 您是否考虑过让您的 XML 解析器将数据插入 SQLite 数据库,然后使用CursorAdapter 将其绑定到您的ListView?这将为您提供更简洁的代码、没有线程问题和更好的性能。

标签: c# java android listview xamarin.android


【解决方案1】:

我认为问题就在这里

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

您无法从非 UI 线程访问 Aapter 类。尝试做这样的事情

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);
        }

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2012-12-11
    相关资源
    最近更新 更多