2016-01-21 157 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

谢谢老兄,没想到那是那么简单 –