AsyncTask 中的 ProgressDialog
我试图在从 HTTP 服务器加载 RSS 源时显示自定义进度对话框,我进行了艰苦的搜索,但没有任何帮助我做到这一点,我唯一知道的是解决方案应该使用 AsyncTask
,但我对传递给此 AsyncTask 的参数感到困惑。 这是我的活动:
public class Soirees extends ListActivity {
private List<Message> messages;
private TextView tvSorties;
private MyProgressDialog dialog;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.sorties);
tvSorties=(TextView)findViewById(R.id.TVTitle);
tvSorties.setText("Programme des soirées");
loadFeed();
}
private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<Message> titles = new ArrayList<Message>(messages.size());
for (Message msg : messages){
titles.add(msg);
}
MessageListAdapter adapter = new MessageListAdapter(this,titles);
this.setListAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Throwable t){
Log.e("ImageLoader",t.getMessage(),t);
}
}
}
您能帮我添加 AsyncTask
吗?
I'm trying to display a custom progressdialog while loading RSS feed from an HTTP server, I made a hard search, but nothing helped me to do this, the only thing I know is that the solution should use AsyncTask
, but I'm confusing about the params to pass to this AsyncTask
.
Here's my activity :
public class Soirees extends ListActivity {
private List<Message> messages;
private TextView tvSorties;
private MyProgressDialog dialog;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.sorties);
tvSorties=(TextView)findViewById(R.id.TVTitle);
tvSorties.setText("Programme des soirées");
loadFeed();
}
private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<Message> titles = new ArrayList<Message>(messages.size());
for (Message msg : messages){
titles.add(msg);
}
MessageListAdapter adapter = new MessageListAdapter(this,titles);
this.setListAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Throwable t){
Log.e("ImageLoader",t.getMessage(),t);
}
}
}
Can you please help me add AsyncTask
to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
通过将视图修饰符移动到 onPostExecute 来修复,因此固定代码是:
@Vladimir,谢谢你的代码非常有帮助。
Fixed by moving the view modifiers to onPostExecute so the fixed code is :
@Vladimir, thx your code was very helpful.
AsyncTask 非常有帮助!
AsyncTask is very helpful!
几天前,我发现了这个问题的一个非常好的解决方案。请在此处阅读相关内容。简而言之,Mike 创建了一个 AsyncTaskManager 来协调 ProgressDialog 和 AsyncTask。使用这个解决方案非常容易。您只需要在项目中包含几个接口和几个类,然后在您的活动中编写一些简单的代码并从 BaseTask 嵌套新的 AsyncTask。我还建议您阅读评论,因为其中有一些有用的提示。
A couple of days ago I found a very nice solution of this problem. Read about it here. In two words Mike created a AsyncTaskManager that mediates ProgressDialog and AsyncTask. It's very easy to use this solution. You just need to include in your project several interfaces and several classes and in your activity write some simple code and nest your new AsyncTask from BaseTask. I also advice you to read comments because there are some useful tips.
不知道应该使用什么参数?
由于参数的模糊性,许多开发人员在开始编写 AsyncTask 时都遇到了困难。很大的原因是我们试图记住 AsyncTask 中使用的参数。关键是不要死记。如果您可以想象您的任务真正需要做什么,那么使用正确的签名编写 AsyncTask 将是小菜一碟。
什么是异步任务?
AsyncTask 是在后台线程中运行的后台任务。它接受输入,执行进度并给出输出。
只需弄清楚您的输入、进度和输出是什么,您就可以开始了。
例如
您如何在代码中编写此内容?
您将如何在您的 Activity 中运行此任务?
Don't know what parameter should I use?
A lot of Developers including have hard time at the beginning writing an AsyncTask because of the ambiguity of the parameters. The big reason is we try to memorize the parameters used in the AsyncTask. The key is Don't memorize. If you can visualize what your task really needs to do then writing the AsyncTask with the correct signature would be a piece of cake.
What is an AsyncTask?
AsyncTask are background task which run in the background thread. It takes an Input, performs Progress and gives Output.
Just figure out what your Input, Progress and Output are and you will be good to go.
For example
How can You write this in a code?
How will you run this Task in Your Activity?
自从提出这个问题以来已经有几年了(自从有人发布回复以来)。从那时起,ProgressDialog 在 API 级别 O 中被弃用,根据 Android 的官方文档。 因此,您可以考虑使用 正如文档作者建议的那样,使用内联进度条而不是 ProgressDialog。
It's been a few years since this question was asked (and since someone has posted a response). Since then, ProgressDialog was deprecated in API level O, according to Android's official documentation. As such, you might consider using an inline progress bar instead of a ProgressDialog as the documentation authors suggest.
这个问题已经得到解答,这里的大多数答案都是正确的,但它们没有解决配置更改的一个主要问题。看看这篇文章 https:// /androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/ 如果您想以更好的方式编写异步任务。
This question is already answered and most of the answers here are correct but they don't solve one major issue with config changes. Have a look at this article https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/ if you would like to write a async task in a better way.