2011-11-16 31 views
2

[更新:感謝Craigy的建議,我清理了一下我的代碼並在onTap中的字符串之前添加了'final',並且一切都很好!]Android MapView - 擴展OverlayItem以存儲訪問onTap的URL

我試圖在我的應用程序的mapview中的自定義OverlayItem中存儲URL(以及標題和片段)。當用戶點擊OverlayItem(在我的情況下是一個NewsMapItem)時,我想要一個對話框來彈出標題和描述。在此之下,有「打開」和「解除」按鈕。當用戶點擊打開按鈕時,我想訪問存儲在NewsMapItem中的URL並將其加載到WebView中。

請參閱下面的相關代碼片段:

NewsMapItem - 在這裏我想在添加鏈接(除了點,標題和片段由默認OverlayItem類提供)。

private class NewsMapItem extends OverlayItem { 
    private String mLink; 
    public NewsMapItem(GeoPoint point, String title, String snippet, String mLink) { 
     super(point, title, snippet); 
     this.mLink = mLink; 
    } 
    public String getLink() { 
     return mLink; 
    } 
} 

中的onTap倍率(我的類擴展ItemizedOverlay內):

@Override 
    protected boolean onTap(int index) { 
     NewsMapItem item = overlays.get(index); 
     AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
     dialog.setTitle(item.getTitle()); 
     dialog.setMessage(item.getSnippet()); 

     // Added the line below... 
     final String url = item.getLink(); 

     dialog.setCancelable(true); 
     dialog.setPositiveButton(R.string.mapview_open, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

       // Removed this call... 
       // String url = item.getLink(); 

       Intent showContent = new Intent(getApplicationContext(), JJGWebViewActivity.class); 
       showContent.setData(Uri.parse(url)); 
       startActivity(showContent); 
      } 
     }); 
     dialog.setNegativeButton(R.string.mapview_dismiss, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
     dialog.show(); 
     return true; 
    } 

項存取的紅色下劃線和Eclipse告訴我:「不能引用非最終變量項目的內部類中用不同的方法定義。「

任何幫助,還是有更好的方式來做我想要的?

爲了簡單起見,我根本無法顯示片段,並將URL存儲在那裏,但我想在對話框中顯示標題和說明,以便用戶可以選擇是否打開故事更容易。

回答