Android的,异步和进度对话框 [英] Android, async and progress dialogs
问题描述
我有下面这段code:
公共类SomeActivity延伸活动{
上下文语境;
名单<菜单项>菜单项;
公共无效importList(视图v){
菜单项=新的ArrayList<菜单项>();
ProgressDialog对话框= ProgressDialog.show(this.context,标题,味精);
MyAsyncTask任务=新MyAsyncTask(上下文); // Context是在这里,因为我试图创建ProgressDialog内pre / postExecute,但它也不管用
task.execute();
尝试 {
//菜单项= task.get();
}赶上(例外五){
//:(
}
}
@覆盖
保护无效的onCreate(包savedInstanceState){
// ...
this.context =这一点;
}
}
在我注释行,在那里我得到的AsyncTask值(菜单项= task.get())万物工作正常。但是,当我取消它,ProgressDialog后出现的任务完成,并返回值。这是为什么呢?
我认为它有某事做这些环境(这就是为什么我包括onCreate方法),但我没有任何想法如何解决它。很显然,我希望ProgressDialog显示前任务完成,而不是之后。
不知道是否有关 - MyAsyncTask做HTTP请求和一些JSON解析
我认为它有某事做这些上下文
不尽然。此外,发送时上下文
从活动
您不必创建一个变量。只需使用这
或 ActivityName.this
。
MyAsyncTask任务=新MyAsyncTask(本);
但是,当我取消它,ProgressDialog后出现的任务完成,并返回值。这是为什么呢?
调用的get()
块中的 UI
,这就是为什么你没有看到的进展,直到它完成。你不需要调用此。 onProgressUpdate的点()
是这样后台线程( doInBackground()
)可以调用它来更新 UI
,因为 doInBackground()
不上的 UI
运行。
从code删除了这一行,从不看它了。如果你认为你需要它,那么请解释一下,我们可以帮你找到更好的方法有原因的。
修改
<一个href="http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648">See这太回答在更新时,任务完成
I have following piece of code:
public class SomeActivity extends Activity {
Context context;
List<MenuItem> menuItems;
public void importList(View v) {
menuItems = new ArrayList<MenuItem>();
ProgressDialog dialog = ProgressDialog.show(this.context, "TITLE", "MSG");
MyAsyncTask task = new MyAsyncTask(context); // Context is here because I tried to create ProgressDialog inside pre/postExecute, but it doesn't work either
task.execute();
try {
// menuItems = task.get();
} catch(Exception e) {
// : (
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
this.context = this;
}
}
When I comment the line, where i get values from AsyncTask ("menuItems = task.get()") everythings work ok. But when I uncomment it, ProgressDialog appears AFTER the task is finished, and value returned. Why is that?
I think that it has sth to do with these contexts (that's why I included onCreate method) but I don't have any idea how to fix it. Obviously, I want ProgressDialog to display BEFORE task is finished, not after.
Not sure if relevant - MyAsyncTask is doing http request and some json parsing.
I think that it has sth to do with these contexts
Not at all. Also, when sending Context
from an Activity
you don't need to create a variable. Simply use this
or ActivityName.this
.
MyAsyncTask task = new MyAsyncTask(this);
But when I uncomment it, ProgressDialog appears AFTER the task is finished, and value returned. Why is that?
Calling get()
blocks the UI
, this is why you don't see the progress until it is done. You don't need to call this. The point of onProgressUpdate()
is so the background thread (doInBackground()
) can call it to update the UI
since doInBackground()
doesn't run on the UI
.
Remove that line from your code and never look at it again. If there is a reason that you think you need it then please explain and we can help you find a better way.
Edit
See this SO answer on updating when the task is finished
这篇关于Android的,异步和进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!