2012-02-21 119 views
-2

我很困惑,因爲這對我來說在其他活動中相當不錯,在這裏我只是基本上覆制粘貼了代碼,但ProgressDialog不顯示。這裏的代碼:ProgressDialog不顯示。再次

public class MyListActivity extends ListActivity { 
    public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.mylayout);   
      final ProgressDialog progress = new ProgressDialog(this);   
      progress.setProgressStyle(STYLE_SPINNER); 
      progress.setIndeterminate(true); 
      progress.setMessage("Working..."); 
      progress.show(); 
      Thread thread = new Thread() 
       {   
        public void run() 
        { 

         //long operation populating the listactivity 
         progress.dismiss(); 
        } 
       }; 
       thread.run();    
     } 
} 

回答

1

不知道這是你的問題的根本原因,但嘗試執行thread.start()而不是thread.run()。執行start()實際上會啓動一個新線程,並且可能會給進度對話框顯示一個機會。

0

您應該使用AsyncTask來管理長操作。

private class LongOperation extends AsyncTask<HttpResponse, Integer, SomeReturnObject> 
{ 
    ProgressDialog pd; 
    long totalSize; 

    @Override 
    protected void onPreExecute() 
    { 
     pd = new ProgressDialog(this); 
     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setMessage("Please wait..."); 
     pd.setCancelable(false); 
     pd.show(); 
    } 

    @Override 
    protected SomeReturnObject doInBackground(HttpResponse... arg0) 
    { 
     // Do long running operation here   
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) 
    { 
     // If you have a long running process that has a progress 
     pd.setProgress((int) (progress[0])); 
    } 

    @Override 
    protected void onPostExecute(SomeReturnObject o) 
    { 
     pd.dismiss(); 
    } 
} 
0

從上面的代碼,它實際上是顯示對話框並立即在Thread run()方法中關閉它。如果你真的想看看它是否顯示了一個Thread.sleep(2000)來測試,但是是的,John Russell說的就是改用AsyncTask的方式。