Search code examples
javaandroiditemizedoverlayoverlayitem

Android MapView - Extending OverlayItem to store a URL for access in onTap


[Update: Thanks to Craigy's suggestion below, I cleaned up my code a little and added 'final' before the string in onTap, and everything works great!]

I'm trying to store a URL (along with a title and snippet) in a custom OverlayItem on my app's mapview. When a user taps on the OverlayItem (in my case, a NewsMapItem), I want a dialog to pop up with the title and description. Below that, there are 'Open' and 'Dismiss' buttons. When the user taps the Open button, I'd like to access a URL stored in the NewsMapItem and load that in a WebView.

Please see the relevant code snippets below:

NewsMapItem - where I'd like to add in a link (in addition to the point, title, and snippet provided by the default OverlayItem class).

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 override (inside my class extending 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;
    }

The item accessors are underlined in red, and Eclipse tells me "Cannot refer to a non-final variable item inside an inner class defined in a different method."

Any help, or is there a better way to do what I'm trying?

For simplicity's sake, I could not display the snippet at all, and store the URL in there, but I'd like to show a title and description in the dialog, so the user can choose whether or not to open a story more easily.


Solution

  • You can only access variables from an inner class in Java if the variables are declared as final (making them constants). More information

    Cannot refer to a non-final variable inside an inner class defined in a different method

    so try putting final in front of the declaration of the variables it complains about, or use the Eclipse code-complete to do it for you.