Android的 - 如何更新ArrayAdapter异步? [英] Android - How to update ArrayAdapter Asynchronously?
问题描述
我使用的是的AsyncTask
类添加,删除和明确的项目我的 ArrayAdapter
。不幸的是,所有这些任务运行时,在 ArrayAdapter
希望 notifyDataSetChanged()
,这就要求线程在 uiThread
运行。
I'm using an AsyncTask
class to add, remove and clear items from my ArrayAdapter
. Unfortunately, when any of those tasks are run, the ArrayAdapter
wants to notifyDataSetChanged()
, which requires that the thread be run on the uiThread
.
如果我用 runOnUiThread(新的Runnable(){...});
,以更新ArrayAdapter异步,不说打败试图为目的还更新了它一个新的线程来开始?
If I use runOnUiThread(new Runnable(){...});
in order to update an ArrayAdapter Asynchronously, doesn't that defeat the purpose of trying to update it in a new Thread to begin with?
什么是最好的办法吗?
推荐答案
不要 .notifyDataSetChanged()
insde onPostExecute()
方法。 onPostExecute()
方法上的UI线程上运行。如果您的应用要求执行doInBackground()
方法,然后用 publishProgress()
(将envoke <期间定期更新code> onProgressUpdate()法),并调用 notifyDataSetChanged()
在 onProgressUpdate()
方法,它也运行在UI线程。
Do .notifyDataSetChanged()
insde onPostExecute()
method. onPostExecute()
method runs on Ui-Thread. If your app requires to periodically update during execution of doInBackground()
method then use publishProgress()
(which will envoke onProgressUpdate()
method) and call notifyDataSetChanged()
inside onProgressUpdate()
method, which also runs on Ui-thread.
这篇关于Android的 - 如何更新ArrayAdapter异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!