2014-04-13 55 views
0

我有一個顯示片段的OnCreateView線程期間progressDialog(自定義)的問題。我想的是,progressDialog是線程的工作(解僱,如果它完成)顯示progressDialog,而線程在片段中執行

// onCreateView 
    @Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

    frg = inflater.inflate(R.layout.services_frag, container, false); 
    pdialog = new TransparentProgressDialog(getActivity(), R.drawable.loading_throbber); 
    //show pdialog 
    pdialog.show(); 
    Thread initializer= new Thread(new Task()); 
    initializer.start(); 
    retun frg; 
    } 

,任務運行的過程中顯示:

 class Task implements Runnable { 
     @Override 
     public void run() { 

      for (int i = 0; i < list.size(); i++) { 
       final int value =i; 
       handler.post(new Runnable() { 
         @Override 
         public void run() { 

        // code works 
        } 
         }); 
        handler.removeCallbacks(this); 
          if(value== list.size()-1) 
          { 
           pdialog.dismiss();       
           } 
       } 
      } 
     } 

該progressDialog顯示了一段時間,在這個問題結束 。

回答

0

你需要換

// code works 

if(value== list.size()-1) 
{ 
    pdialog.dismiss();       
} 

因爲handler.post(...)代碼將在UI線程中執行