开发者

progressdialog shows up to late (async task)

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);

            }
        });开发者_StackOverflow

    }


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜