Android:线程从网络加载数据后更新列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/651733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:22:29  来源:igfitidea点击:

Android: Update Listview after Thread loads data from the net

androidlistview

提问by Trevor

  1. I like that my GUI appears immediately when the user starts the app.
  2. Then some data (text, pictures) gets loaded in the background (like YouTube app).
  3. The ListView and Gallery gets updated automatically with this new data.
  1. 我喜欢我的 GUI 在用户启动应用程序时立即出现。
  2. 然后在后台加载一些数据(文本、图片)(如 YouTube 应用程序)。
  3. ListView 和 Gallery 会使用这些新数据自动更新。

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).

我启动我的 ListView,启动一个线程并加载数据......然后 ListView 没有更新!有几个人告诉我应该使用notifyDataSetChanged(). 但是我不能将此命令放在我的线程中(只是未知)。

Any ideas?

有任何想法吗?

回答by Trevor

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:

之后,让问题坐了大约两周,我在这里找到了解决方案

Long story short:

长话短说:

Quote from above link:

引用上面的链接:

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.

我们必须使用 Handler 对象,因为我们无法在单独的线程中更新大多数 UI 对象。当我们向 Handler 发送消息时,它将被保存到队列中并尽快由 UI 线程执行。

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:

一旦你检查了代码,你就会明白作者在说什么。
注意:即使使用处理程序,Android 也可能不允许您从线程的 run() 方法更新视图对象。我收到此错误:

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.

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.

为了解决这个问题,我在 run() 方法中更新了一个数据数组,并使用该数组来更新处理程序的 handleMessage() 方法中的视图。

I hope this helps others out there.

我希望这可以帮助其他人。

回答by Trevor

You may use the slowAdapter to refresh the View:

您可以使用 slowAdapter 刷新视图:

SlowAdapter slowAdapter = new SlowAdapter(this);

list.setAdapter(slowAdapter);
slowAdapter.notifyDataSetChanged();

回答by digitarald

Just found it myself while reading this thread and trying around.

只是在阅读此线程并尝试时自己发现了它。

Short: AsyncTask's method onProgressUpdatecan touch the view: http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)

简短:AsyncTaskonProgressUpdate方法可以触摸视图http: //developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)

Background: I needed to call requery on my cursor so a ListView kept being updated while the task fills the database. The requery call made in doInBackgroundfailed with the mentioned CalledFromWrongThreadExceptionbut same code in onProgressUpdateworks.

背景:我需要在我的光标上调用 requery,以便在任务填充数据库时不断更新 ListView。在doInBackground 中进行的重新查询调用因提到的CalledFromWrongThreadException而失败,但onProgressUpdate 中的相同代码有效。