2012-08-06 46 views
1

我想知道如何找出哪些活動(類名)開始了我的自定義ItemizedOverlay類,從AlertDialog在我的自定義ItemizedOverlay類。我對不同地方的MapView有不同的活動。加載MapView時,所有活動將自動啓動ItemizedOverlay類。因此,我不能把意圖附加在意圖之外。如何知道哪些活動開始ItemizedOverlay類

任何人都知道這是否可能?

這裏是我的ItemizedOverlay構造類(請忽略註釋部分,併爲alertdialog消息部分):

public class CustomItemisedOverlay extends ItemizedOverlay<OverlayItem> { 

private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>(); 

private Context context; 

public CustomItemisedOverlay(Drawable defaultMarker) { 
    super(boundCenterBottom(defaultMarker)); 
    // TODO Auto-generated constructor stub 
} 

public CustomItemisedOverlay(Drawable defaultMarker, Context context) { 
    this(defaultMarker); 
    this.context = context; 
} 

@Override 
protected OverlayItem createItem(int i) { 
    // TODO Auto-generated method stub 
    return mapOverlays.get(i); 
} 

@Override 
public int size() { 
    // TODO Auto-generated method stub 
    return mapOverlays.size(); 
} 

//AlertDialog for driving directions here 
@Override 
protected boolean onTap(int index) { 
    AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
    //Title of AlertDialog 
    dialog.setTitle("Driving Directions"); 
    //Message of AlertDialog 
    String className = getClass().getSimpleName().toString(); 
    dialog.setMessage(className); 
    //Positive Button 
    dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // Handle launch of driving directions here 
      /*String tappedLong = null; 
      String tappedLat = null; 
      String className = this.getClass().getSimpleName().toString(); 
      if(className == "amkActivity") { 
       tappedLong = "1.363414"; 
       tappedLat = "103.9370256"; 

      } else if (className == "bedokActivity") { 
       tappedLong = "1.3248498"; 
       tappedLat = "103.9370256"; 
      } 


      Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
        Uri.parse("http://maps.google.com/maps?daddr=" + tappedLat + "," + tappedLong)); 
      context.startActivity(intent);*/ 

     } 
    }); 

    //Negative Button 
    dialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      dialog.cancel(); 
     } 
    }); 
    //Display AlertDialog when tapped 
    dialog.show(); 
    return true; 
} 

public void addOverlay(OverlayItem overlay) { 
    mapOverlays.add(overlay); 
    this.populate(); 
} 

}

回答

0

使用

mapView.getContext(); 

在itemizedoverlay的構造..

這將返回MapView的頁面的背景下...

+0

您指的是什麼mapView?似乎無法得到它.. =( – SilverHood 2012-08-06 08:22:08

+0

我只能用你的建議是什麼在MapView類,如何在我的自定義ItemizedOverlay類使用它在alertdialog? – SilverHood 2012-08-06 08:26:28

+0

我正在期運用定製itemizedoverlay ..並在構造函數。逐項疊加我有通行證的MapView對象作爲參數,我已經使用mapview.getcontext();在構造函數... – 2012-08-06 08:27:30

0

我也不太清楚如何工作的,但要獲得啓動該CustomItemisedOverlay類活動的名稱,使用

context.getClass().getSimpleName(); 

例如,我希望把活動名稱到一個名爲字符串類名,我這樣做:

String className = context.getClass().getSimpleName().toString(); 

爲什麼使用context原因是因爲該行的代碼(可以在上面找到):

private Context context; 

因此context這裏指的是啓動CustomItemisedOverlay類的活動的上下文