2011-04-13 67 views
0

當我定義進展對話功能,例如在Android中,從互聯網上獲取數據期間ProgressDialog.show()和ProgressDialog.hide()的問題的usege

public static void showLoadingBar(Context context) 
{ 
    dialog=new ProgressDialog(context); 
    dialog.setMessage("Please wait"); 
    dialog.show(); 
} 
public static void hideLoadingBar() 
{ 
    dialog.dismiss(); 

} 

我想使用它像以下:

UiManager.getInstance().showLoadingBar(this); 
    FetchData(); 
    UiManager.getInstance().hideLoadingBar(); 

但我從來沒有能夠顯示LoadingBar,直到我評論 UiManager.getInstance()。hideLoadingBar();線如此

UiManager.getInstance().showLoadingBar(this); 
    FetchData(); 
    //UiManager.getInstance().hideLoadingBar(); 

這是什麼原因,總是在屏幕上ProgressBar。無論如何要擺脫這個問題?

回答

1

FetchData()似乎是一個異步操作。所以,在實際操作完成之前,函數返回並隱藏加載條。我建議你使用AsyncTask

要顯示一個進度對話框的同時運行的AsyncTask,您可以撥打show()onPreExecute()onPostExecute()調用hide()。請致電FetchData()doInBackground()。這將在AsyncTask執行後臺方法之前啓動ProgressDialog,並在完成時停止ProgressDialog。

+0

秒吧。 @Muratcan當你的'FetchData()'返回時,如果這是一個異步操作,你應該隱藏進度條。 – Varun 2011-04-13 08:55:15