Java 刷新视图 Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1021167/
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-11 22:20:53  来源:igfitidea点击:

Refresh a view Android

javaandroid

提问by Sam97305421562

I want to refresh a listview. When I am getting a value from different class I want it to update that to list view. I am creating a thread for refreshing the list but I am getting an exception:

我想刷新一个列表视图。当我从不同的类获取值时,我希望它更新到列表视图。我正在创建一个线程来刷新列表,但出现异常:

Only the original thread that created a thread a view hierarchy can touch its views

Only the original thread that created a thread a view hierarchy can touch its views

How to proceed?

如何进行?

采纳答案by Prashast

Instead of trying to refresh the view to update the list just take the Adapter object and call the notifyDataSetChanged() API.

无需尝试刷新视图来更新列表,只需获取 Adapter 对象并调用 notifyDataSetChanged() API。

回答by Josef Pfleger

Whenever you work with multiple threads in an Android GUI you should use Handlers. The Android Dev Guide has a good example of how to use Handlers with a ProgressDialogwhen you expand the subsection Example ProgressDialog with a second threadhere.

每当您在 Android GUI 中使用多个线程时,您都应该使用Handlers。Android Dev Guide 有一个很好的示例,说明当您在此处使用第二个线程展开示例 ProgressDialog小节时,如何将Handlers 与 a一起使用。ProgressDialog

The example should be easy to understand and help you to apply the same concept to your list view.

该示例应该易于理解并帮助您将相同的概念应用于您的列表视图。