2011-09-28 54 views
1

我感谢一个asynctask动态更新我的UI。 在onpreexecute方法上,我显示了progressdialog,但它显示到最近。progressdialog显示到最近(异步任务)

我的意思是,我点击一个按钮来改变活动,然后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); 

      } 
     }); 

    } 
+0

如果你解决了你的问题PLZ把你的答案。我面临同样的问题。 – Hemant

回答

0

尽量显示你的ProgressDialog只是执行AsyncTask之前。这通常会有所帮助。

+0

随着我的异步任务之前的progressdalog,我有同样的行为:我点击一个按钮来更改活动,然后UI被冻结,并且我简要地看到progressdialog并最终更新了我的UI。虽然我会:我点击确定按钮,我看到进度对话框,然后用户界面被更新:-)。 – Miroufle

0

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

+0

我尝试过但没有改变,问题出现在活动开始时(我猜)。我的progressdialog消失,因为我想要的。谢谢你的帮助。 – Miroufle