进度对话框不会立即出现

发布于 2024-10-14 02:46:24 字数 1282 浏览 2 评论 0原文

当我开始新活动时,我的进度对话框不会立即出现。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮是我掰弯的 2024-10-21 02:46:24
Handler mHandler = new Handler();// This statement is to be called by the main thread  

ProgressDialog.show();// This statement is to be called by the main thread  

Thread t = new Thread(  
new Runnable(){  

public void run()  
{  

  callWebServicesHereAndFetchingMethod(); //  

   mHandler.post(new Runnable(){  

   public void run()  
   {  
     ProgressDialog.dismiss();  
   }   
});  
    }});  
t.start();  
Handler mHandler = new Handler();// This statement is to be called by the main thread  

ProgressDialog.show();// This statement is to be called by the main thread  

Thread t = new Thread(  
new Runnable(){  

public void run()  
{  

  callWebServicesHereAndFetchingMethod(); //  

   mHandler.post(new Runnable(){  

   public void run()  
   {  
     ProgressDialog.dismiss();  
   }   
});  
    }});  
t.start();  
小鸟爱天空丶 2024-10-21 02:46:24

您的代码需要多线程......所有视觉效果都由您的主线程控制。如果您使用主线程进行任何处理或从 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

雾里花 2024-10-21 02:46:24

用户界面 (UI) 不是线程安全的,对 UI 的调用必须从主线程(也称为“UI 线程”)进行。

Handler handler = new Handler(); // Create on main thread.

// ...

handler.post(new Runnable() {
  @Override
  public void run() {
    ProgressDialog dialog = ProgressDialog.show(this, "",
       "Loading. Please wait...", true);
    }
});

The user interface (UI) is not thread safe, calls to the UI must be made from the main thread (also called the "UI thread").

Handler handler = new Handler(); // Create on main thread.

// ...

handler.post(new Runnable() {
  @Override
  public void run() {
    ProgressDialog dialog = ProgressDialog.show(this, "",
       "Loading. Please wait...", true);
    }
});
不羁少年 2024-10-21 02:46:24

您正在寻找的是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文