【发布时间】:2011-11-16 17:14:14
【问题描述】:
[更新:感谢 Craigy 在下面的建议,我稍微清理了我的代码并在 onTap 中的字符串之前添加了“final”,一切正常!]
我正在尝试将 URL(连同标题和 sn-p)存储在我的应用程序地图视图上的自定义 OverlayItem 中。当用户点击 OverlayItem(在我的例子中是 NewsMapItem)时,我希望弹出一个带有标题和描述的对话框。在其下方,有“打开”和“关闭”按钮。当用户点击打开按钮时,我想访问存储在 NewsMapItem 中的 URL 并将其加载到 WebView 中。
请看下面的相关代码sn-ps:
NewsMapItem - 我想在其中添加链接(除了默认 OverlayItem 类提供的点、标题和 sn-p)。
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;
}
item 访问器带有红色下划线,Eclipse 告诉我“不能引用在不同方法中定义的内部类中的非 final 变量项。”
任何帮助,或者有更好的方法来做我正在尝试的事情吗?
为简单起见,我根本无法显示 sn-p,并将 URL 存储在其中,但我想在对话框中显示标题和描述,以便用户选择是否打开一个故事更容易。
【问题讨论】:
标签: java android itemizedoverlay overlayitem