进度对话框不会立即出现
当我开始新活动时,我的进度对话框不会立即出现。我正在使用 AsyncTask 来实现同样的目的。我将在下一个活动中从网络服务加载数据。以下是我的异步类:
private class TheTask extends AsyncTask<Void, Void, Void>{
Context con;
Intent aboutusIntent;
TabGroupActivity parentActivity;
private TheTask(Context context)
{
this.con=context;
}
@Override
protected void onPreExecute() {
progDialog = ProgressDialog.show(con, "Loading... ",
"please wait....", true);
}
@Override
protected Void doInBackground(Void... params) {
aboutusIntent = new Intent(con, Departments.class);
parentActivity = (TabGroupActivity)getParent();
//progDialog.show();
return null;
}
@Override
protected void onPostExecute(Void result) {
parentActivity.startChildActivity("Departments", aboutusIntent);
if(progDialog.isShowing())
{
progDialog.dismiss();
}
}
}
我正在创建此类的实例 onClick 按钮:
ourdepbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TheTask(AboutUs.this.getParent()).execute();
}
});
有什么建议吗?
My progressDialog doesn't appear immediately when I start a new activity. I am using AsyncTask for the same. I am loading data from web service in next activity. Following is my async class :
private class TheTask extends AsyncTask<Void, Void, Void>{
Context con;
Intent aboutusIntent;
TabGroupActivity parentActivity;
private TheTask(Context context)
{
this.con=context;
}
@Override
protected void onPreExecute() {
progDialog = ProgressDialog.show(con, "Loading... ",
"please wait....", true);
}
@Override
protected Void doInBackground(Void... params) {
aboutusIntent = new Intent(con, Departments.class);
parentActivity = (TabGroupActivity)getParent();
//progDialog.show();
return null;
}
@Override
protected void onPostExecute(Void result) {
parentActivity.startChildActivity("Departments", aboutusIntent);
if(progDialog.isShowing())
{
progDialog.dismiss();
}
}
}
I am creating instance of this class onClick of button :
ourdepbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TheTask(AboutUs.this.getParent()).execute();
}
});
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码需要多线程......所有视觉效果都由您的主线程控制。如果您使用主线程进行任何处理或从 Web 服务中提取数据,则不会出现进度对话框。
使进度对话框显示主线程调用的函数。使用另一个线程完成所有的获取。创建一个将加入您的获取线程的线程,并使用 Handler 类对象产生您想要的任何视觉效果(
如果您需要对此进行详细说明)。我也会发布那个
Your Code Need Multithreading .... All the Visual Effect is controlled by your main thread. If you do any processing or say data fectching from web services using Main thread then progress dialog will not appear .
Make the progress dialog show function called by the Main Thread. do all the fetching using another thread. make a thread that will join your fetching thread and Using Handler class object produce any visual effect you want to do
If you need this to be elaborated more. i will post that too
用户界面 (UI) 不是线程安全的,对 UI 的调用必须从主线程(也称为“UI 线程”)进行。
The user interface (UI) is not thread safe, calls to the UI must be made from the main thread (also called the "UI thread").
您正在寻找的是 AsyncTask,您可以在其中显示/隐藏 onPreExecute()/onPostExecute() 中的 ProgressDialog
您可以阅读有关它的更多信息 此处
What you are looking for is AsyncTask, where you can show/hide the ProgressDialog in onPreExecute()/onPostExecute()
You can read more about it here