2013-10-24 52 views
1

我正在讀取lat/lon文件,創建多邊形並將其添加到AsyncTask的地圖中。一切正常 - 除了progressDialog八方通凍結在行動開始,並駁回後動作completed.This是我的代碼:ProgressDialog凍結在異步任務

更新的代碼:

private class shpLoading extends AsyncTask<GoogleMap, List<LatLng>, String> { 
       private ProgressDialog dialog; 
       private String path; 
         private int loaded; 
       public void setPath(String p){ 
        this.path = p; 
       } 

       @Override 
       protected String doInBackground(GoogleMap... params) {  

        final ShpReader shpRead = new ShpReader(); 



         try { 
          shpRead.read(path); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (InvalidShapeFileException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 


          dialog.setMax(shpRead.daugiakampiai().size()); 
          int i = 0; 
          for(List<LatLng> a: shpRead.polygons()){                
           function.arAntLinijos(a); 
           for(int h = 0; h < a.size()-1; h++){ 
            LatLng point = function.midPoint(a.get(h), a.get(h+1)); 
            a.add(h+1, point); 
            h++; 

            if(i > 3000){ 
             message("You reached max count!"); 
             break; 
            } 
           } 
           publishProgress(a); 

           i++;       
          } 

        return "Done"; 
       } 
        @Override 
       protected void onPreExecute() {                   
         dialog = new ProgressDialog(Measuring.this); 
         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
         dialog.setMessage(getString(R.string.loading)); 
         dialog.setCancelable(false); 
         dialog.setProgress(0);       
         dialog.show(); 
       }          
       } 

       @Override 
       protected void onProgressUpdate(List<LatLng>... values) { 
          dialog.setProgress(loaded++); 
       Polygon daug = mMap.addPolygon(new PolygonOptions() 
        .addAll(values[0]) 
        .strokeWidth(1) 
        .strokeColor(Color.RED) 
        .fillColor(0x3F00FF00)); 

        plotai.add(new Polygons(db.getMaxFieldID()+1, daug)); 
        daug = null;        
       } 

       @Override 
       protected void onPostExecute(String result) { 
        dialog.dismiss(); 

       }      
     } 

回答

2

因爲你要提醒你的多邊形不onProgressUpdate方法,但是在doInBackground方法。在onProgressUpdate方法中,您必須更新您的ProgressDialog對象中的進度(在您的代碼中顯示ProgressDialog,但根本沒有更新其進度)。

+0

我畫他們在'doInBackground'使用處理程序 - 同樣的結果 – Eddwhis

+0

在onProgressUpdate方法添加「dialog.setProgress(progressValue)」,其中progressValue將電流加載進度(例如:百分數) –

+0

不,它不也行不通。進度條在0處凍結,最後直接跳到100% – Eddwhis