progressdialog显示最新(异步任务)

时间:2011-09-28 20:26:07

标签: android android-asynctask progressdialog

由于asynctask,我动态更新了我的UI。 在onpreexecute方法中,我显示了progressdialog,但它显示为最晚。

我的意思是,我点击一个按钮来更改活动,然后UI被冻结,然后我简要地看到progressdialog,最后我的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);

            }
        });

    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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