I'm trying to get a class extending ItemizedOverlay to startActivity but there is a problem, it just won't compile. I have a MapView that uses the Ite开发者_如何学CmizedOverlay class to draw overlays but i want to start and activity when i tap on the screen.
Any idea how to solve this problem? Thanks
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
String split_items = item.getSnippet();
Intent intent = new Intent();
intent.setClass(mainmenu,poiview.class);
startActivity(intent);
return true;
}
I had this problem so I have tested the example below. The solution relies on calling "startActivity" from the MapActivity context.
If your map is actually working with overlays then you have already passed the MapView Context to your custom ItemizedOverlay constructor and you probably assigned the MapView Context to a class variable called mContext (I'm making assumptions that you followed Google's MapView example). So in your custom Overlay's onTap function, do this:
@Override
protected boolean onTap(int index) {
Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
mContext.startActivity(intent);
return true;
}
But you probably want to pass something to the new Activity you are trying to launch so your new activity can do something useful with your selection. So...
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
//assumption: you decided to store an "id" in the snippet so you can associate this map location with your new Activity
long id = Long.parseLong(item.getSnippet()); //Snippet is a built-in String property of an Overlay object.
//pass an "id" to the class so you can query
Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
String action = Intent.ACTION_PICK; //You can substitute with any action that is relevant to the class you are calling
//I create a URI this way because I append the id to the end of the URI (lookup the NotePad example for help because there are many ways to build a URI)
Uri uri = ContentUris.withAppendedId(Your_CONTENT_URI, id);
//set the action and data for this Intent
intent.setAction(action);
intent.setData(uri);
//call the class
mContext.startActivity(intent);
return true;
}
Try this one , i have done it with setting a positive button in the alert dialog...
protected boolean onTap(int index) {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setIcon(R.drawable.info_icon);
dialog.setPositiveButton("Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent placeDetailsMapIntent = new Intent(mContext, PlaceDetailsActivity.class);
mContext.startActivity(placeDetailsMapIntent);
}
});
try to use following
Intent intent = new Intent(mainmenu.this, poview.class);
startActivity(intent);
精彩评论