安卓:更新列表视图后,从净线程加载数据
- 我喜欢我的GUI,立即在用户启动应用程序出现。
- 在随后的一些数据(
文字,图片
)的背景下(像YouTube应用程序
)被加载。 - 的ListView和画廊得到这个新的数据自动更新。
- I like that my GUI appears immediately when the user starts the app.
- Then some data (
text, pictures
) gets loaded in the background (like YouTube app
). - The ListView and Gallery gets updated automatically with this new data.
我开始我的ListView,启动一个线程和加载数据...然后在ListView没有更新!
有几个人告诉我,我应该用 notifyDataSetChanged()
。
但我不能在我的主题把这个命令(只是未知)。
I initiate my ListView, start a Thread and load the data... and then the ListView does not get updated!
Several people told me I should use notifyDataSetChanged()
.
But I cannot place this command in my Thread (just unknown).
任何想法?它可以不那么辛苦,但我要疯了.. 如果需要,我会给你我的源$ C $ C。
Any ideas? It can't be so hard, but I am going crazy.. I will give you my source code if needed.
我有这个同样的问题......我很兴奋,当我碰到这个问题。但是,没有答案? : - (
I have this same problem... and I got excited when I came across this question. But no answer? :-(
后,让问题坐了大约两个星期,我发现了解决方案在这里:
After, letting the problem sit for about two weeks I found the solution here:
长话短说:
从上面的链接引用:
我们必须使用,因为Handler对象 我们不能更新,而大多数用户界面对象 在一个单独的线程。当我们发送一个 消息处理程序,将得到 保存到一个队列,并获得由执行 尽快UI线程。
We must use a Handler object because we cannot update most UI objects while in a separate thread. When we send a message to the Handler it will get saved into a queue and get executed by the UI thread as soon as possible.
在您检查出code,你看得到什么笔者说。
注:即使有一个处理程序,机器人可能不会让你更新从线程的run()方法的视图对象。
我得到这个错误:
Once you check out the code you see get what the author is saying.
NOTE: Even with a handler, Android may not let you update a view object from the thread's run() method.
I got this error:
05-31 02:12:17.064: ERROR/AndroidRuntime(881):
android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
要解决它,我更新的数据数组在我的run()方法,并用该数组更新处理程序的handleMessage()函数方法的看法。
To get around it I updated an array of data in my run() method and used that array to update the view in the handler's handleMessage() method.
我希望这可以帮助其他人那里。
I hope this helps others out there.