我想知道如何从我的自定义ItemizedOverlay类的AlertDialog中找出哪个活动(类名)启动了我的自定义ItemizedOverlay类。我与不同地方的MapView有不同的活动。加载MapView时,所有活动都将自动启动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 :(得分:0)
使用
mapView.getContext();
这将返回mapview页面的上下文...
答案 1 :(得分:0)
我不太清楚这是如何工作的,但要获取启动CustomItemisedOverlay类的活动的名称,请使用
context.getClass().getSimpleName();
例如,我想将活动名称放入名为className的String中,我这样做:
String className = context.getClass().getSimpleName().toString();
使用context
的原因是因为这行代码(可以在上面找到):
private Context context;
因此context
指的是启动CustomItemisedOverlay类的活动的上下文