2011-09-28 56 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