2016-01-21 98 views
0

我有一個選項卡式活動,使用ViewPager瀏覽三個片段。 當我加載一個片段時,我不得不加載一些數據,所以我把加載代碼放在一個AsyncTask中,我想在加載數據時顯示一個ProgressDialog。 這是我的AsyncTask代碼:ViewPager片段不顯示ProgressDialog

public GetGeneralitaTask(Context c){ 
      this.c=c; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog=new ProgressDialog(c); 
     progressDialog.setMessage("Caricamento..."); 
     progressDialog.setIndeterminate(true); 
     progressDialog.setCancelable(false); 
     progressDialog.show(); 
    }@Override 
    protected String doInBackground(Void... params) { 

     the loading part.... 
    } 
    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 

     progressDialog.dismiss(); 
     Log.e("ending ", "second task"); 
    } 

然後在片段onCreateView我打電話:

getGeneralitaTask=new GetGeneralitaTask(getActivity()); 
    getGeneralitaTask.execute(); 
    try { 
     getGeneralitaTask.get(); 
    } catch (InterruptedException | ExecutionException e) { 
     e.printStackTrace(); 
    } 

,但沒有顯示...的wiew只是靜止不動,直到該數據被加載並填充視圖

回答

1

的問題是在這條線:

getGeneralitaTask.get();

它會導致當前線程等待任務完成。

+0

謝謝老兄,沒想到那是那麼簡單 –