进度对话框显示到最晚(异步任务)

发布于 2024-12-07 05:28:43 字数 2002 浏览 0 评论 0原文

由于异步任务,我动态更新了我的 UI。 在 onpreexecute 方法中,我显示了进度对话框,但它显示得很晚。

我的意思是,我单击一个按钮来更改活动,然后 UI 被冻结,之后我简要地看到进度对话框,最后我的 UI 更新了。

请问我的代码有什么问题吗?

private ProgressDialog pd ;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);

    new taskDoSomething().execute();
}

private class taskDoSomething extends AsyncTask<Long, Integer, Integer> 
{

     protected void onPreExecute() {
       // TODO Auto-generated method stub
        pd = ProgressDialog.show(SQLView.this, "Working..", "Calculating", true,true);
      }


 protected Integer doInBackground(Long... params) {
        UpdateIHM();
        return 0;
    }

    protected void onPostExecute(Integer result) {
           Toast.makeText(SQLView.this,"onPostExecute", Toast.LENGTH_LONG).show();
    }
    }


 public void UpdateIHM()
    {

        runOnUiThread(new Runnable() {
           @Override
           public void run() {

            LinearLayout my_layout = (LinearLayout) findViewById(R.id.lil_content);

            HotorNot info  = new HotorNot(SQLView.this);
            my_table data = new my_table();
            Log.i("Test","ok 0");
            info.open();
            Log.i("Test","ok 0.1");


            for(int i =0; i<3; i++){
                data = info.getData();
                for (int j=0; j<50; j++){
                LinearLayout lil_temp = new LinearLayout(SQLView.this); 
                    lil_temp.setOrientation(LinearLayout.HORIZONTAL);
                    lil_temp.addView(buildText(data.row), getParams(WRAP, WRAP));
                    lil_temp.addView(buildText(data.name), getParams(WRAP, WRAP));
                    lil_temp.addView(buildText(data.hotness), getParams(WRAP, WRAP));
                    my_layout.addView(lil_temp);
                }
            }

            info.close();
            handler.sendEmptyMessage(102);

            }
        });

    }

I update dynamically my UI thanks to an asynctask.
On the onpreexecute method I show the progressdialog, but it shows up to late.

I mean, I click on a button to change of activity, then the UI is frozen, and ater I see briefly the progressdialog and finally my UI updated.

What is wrong in my code please ?

private ProgressDialog pd ;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);

    new taskDoSomething().execute();
}

private class taskDoSomething extends AsyncTask<Long, Integer, Integer> 
{

     protected void onPreExecute() {
       // TODO Auto-generated method stub
        pd = ProgressDialog.show(SQLView.this, "Working..", "Calculating", true,true);
      }


 protected Integer doInBackground(Long... params) {
        UpdateIHM();
        return 0;
    }

    protected void onPostExecute(Integer result) {
           Toast.makeText(SQLView.this,"onPostExecute", Toast.LENGTH_LONG).show();
    }
    }


 public void UpdateIHM()
    {

        runOnUiThread(new Runnable() {
           @Override
           public void run() {

            LinearLayout my_layout = (LinearLayout) findViewById(R.id.lil_content);

            HotorNot info  = new HotorNot(SQLView.this);
            my_table data = new my_table();
            Log.i("Test","ok 0");
            info.open();
            Log.i("Test","ok 0.1");


            for(int i =0; i<3; i++){
                data = info.getData();
                for (int j=0; j<50; j++){
                LinearLayout lil_temp = new LinearLayout(SQLView.this); 
                    lil_temp.setOrientation(LinearLayout.HORIZONTAL);
                    lil_temp.addView(buildText(data.row), getParams(WRAP, WRAP));
                    lil_temp.addView(buildText(data.name), getParams(WRAP, WRAP));
                    lil_temp.addView(buildText(data.hotness), getParams(WRAP, WRAP));
                    my_layout.addView(lil_temp);
                }
            }

            info.close();
            handler.sendEmptyMessage(102);

            }
        });

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

讽刺将军 2024-12-14 05:28:43

尝试在执行 AsyncTask 之前显示您的 ProgressDialog。这通常会有帮助。

Try showing your ProgressDialog just before executing the AsyncTask. That'll usually help.

倦话 2024-12-14 05:28:43

我正在做同样的事情,只是我在 onPostExecute() 的开头调用 ProgressDialog.dismiss() 。

I am doing the same thing except that I call ProgressDialog.dismiss() at the beginning of onPostExecute().

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