从ItemizedOverlay类启动Activity

Cid*_*Cid 1 android

我试图让一个类将ItemizedOverlay扩展到startActivity但是有一个问题,它只是不会编译.我有一个MapView,它使用ItemizedOverlay类来绘制叠加层,但我想在我点击屏幕时启动和活动.

知道如何解决这个问题吗?谢谢

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;
   }
Run Code Online (Sandbox Code Playgroud)

Cir*_*716 5

我有这个问题,所以我测试了下面的例子.该解决方案依赖于从MapActivity上下文调用"startActivity".

如果您的地图实际上正在使用叠加层,那么您已经将MapView上下文传递给自定义的ItemizedOverlay构造函数,并且您可能已将MapView上下文分配给名为mContext的类变量(我假设您遵循Google的MapView示例).所以在你的自定义Overlay的onTap函数中,执行以下操作:

        @Override
    protected boolean onTap(int index) {

      Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
      mContext.startActivity(intent);


      return true;
    }   
Run Code Online (Sandbox Code Playgroud)

但是您可能希望将某些内容传递给您尝试启动的新活动,以便您的新活动可以对您的选择做一些有用的事情.所以...

    @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;
}
Run Code Online (Sandbox Code Playgroud)