2012-11-02 28 views
0

在我的應用程序中,我爲該主要活動使用了Google地圖,並在Googlemaps上爲該覆蓋類提供了免費手繪草圖。對於這個免費的手繪草圖來存儲路徑,一個數組列表被用於覆蓋類。接下來在主要活動中進行撤銷和重做功能,因此我必須在主要活動中使用數組列表,因爲我將該數組列表作爲公共靜態聲明爲覆蓋類中的數組列表。與靜態的使用,我得到連續拉拔這是我的問題..如何使用我的應用程序靜態使用數組列表,用了....Android如何爲所有類和活動使用單個數組列表?

public class HandDrawOverlay extends Overlay { 

    private boolean iseditMode = true; 
    private boolean isTouched = false; 
    private Paint paint = new Paint(); 
    private Point screenPt1 = new Point(); 
    private Point screenPt2 = new Point(); 
    public static ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();  
    int x, y; 
    GeoPoint geoP; 
    HandDrawOverlay handDrawOverlay; 
    private GoogleMapActivity mview = null; 
    private boolean isUp = false;  

    public HandDrawOverlay(GoogleMapActivity mapviewdata){ 

     paint.setStrokeWidth(4.0f); 
     paint.setStyle(Style.STROKE); 
     paint.setColor(Color.BLUE); 
     mview = mapviewdata;  
    } 

    public static ArrayList<GeoPoint> getBitmap(){ 
      return points; 
    } 

    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
    if (points != null && points.size() > 1) {   
     mapView.getProjection().toPixels(points.get(0), screenPt1); 

     for (int i = 1; i < points.size(); i++) {    
      mapView.getProjection().toPixels(points.get(i), screenPt2); 
      canvas.drawLine(screenPt1.x, screenPt1.y, screenPt2.x, screenPt2.y, paint); 
      screenPt1.set(screenPt2.x, screenPt2.y);     
     } 
    } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent e, MapView mapView) { 
    if(mview.isEditMode() && (iseditMode)){   

     x = (int) e.getX(); 
     y = (int) e.getY();   
     geoP = mapView.getProjection().fromPixels(x, y);  
     Log.i("", "geoP" +geoP); 

     switch (e.getAction()) { 
     case MotionEvent.ACTION_DOWN:        
      isTouched = true;   
      points.add(geoP);        
      break;    

     case MotionEvent.ACTION_MOVE:    
      if (isTouched = true) 
       points.add(geoP);    
      break; 

     case MotionEvent.ACTION_UP:   
      if (isTouched = true) 
       points.add(geoP);      
      isUp = true; 
      handDrawOverlay = new HandDrawOverlay(mview); 
      mapView.getOverlays().add(handDrawOverlay); 
      break; 
     }   
     mapView.postInvalidate();  
     return true;    
     }  
     return false; 
    }  
} 

回答

0

對於您可以使用此。

public class Hiroshima { 

    private static Hiroshima hr = new Hiroshima(); 


    public ArrayList<String> mImages = new ArrayList<String>(); 


     private Hiroshima(){} 

     public static Hiroshima getInstance(){ 

      return hr; 
     } 

} 

現在你可以在任何地方使用這個ArrayList,它會給你同樣的實例。

在你的活動中記下如下。

Hiroshima hx = Hiroshima.getInstance(); 
+0

什麼代表'hx' – NagarjunaReddy

+0

hx是該類的參考。要使用ArrayList,您必須編寫hx.mImages。 –

+0

thanx快速回復.... – NagarjunaReddy

相關問題