从ItemizedOverlay类启动Activity

时间:2011-01-02 03:58:38

标签: android

我正在尝试将ItemizedOverlay扩展到startActivity但是有一个问题,它只是无法编译。 我有一个使用ItemizedOverlay类来绘制叠加层的MapView,但是当我点击屏幕时,我想要开始和活动。

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

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;
   }

3 个答案:

答案 0 :(得分: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;
    }   

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

    @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;
}

答案 1 :(得分:0)

尝试这个,我已经在警告对话框中设置了一个正面按钮...

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);
         }

});

答案 2 :(得分:-1)

尝试使用以下

Intent intent = new Intent(mainmenu.this, poview.class);
startActivity(intent);