简体   繁体   中英

Android - Getting notified when event happens on a different class

I have an app which has a map and some POI on it. I'm using http://djsolid.net/blog/android---draw-a-path-array-of-points-in-mapview for drawing the path. The Pin markers are using https://github.com/jgilfelt/android-mapviewballoons to display the popup balloon with information

I'm trying to find a way to get notified in the main activity, once

protected boolean onBalloonTap(int index, OverlayItem item) 

occurs. Everything works, regarding getting the points, drawing the balloons and popup info and when tapped (the balloon) it draws the path. however I want to store the selected POI (there are some on the screen, I want to know which one the user selected), But since it happens on different classes, not necessarily when created, I just use a public getter of something like that. The code for creating the overlays is:

    Drawable marker = getResources().getDrawable(R.drawable.malls);
    PinsOverlay malls= new PinsOverlay(marker, mapView);
    List<Overlay> mapOverlays= mapView.getOverlays();
    for (int i = 0; i < coordinates.length; i++) {
        long systemTime = Long.valueOf(currentTime);
        long timeSinceReported = (systemTime - Long.valueOf(time[i])) / 60;
        OverlayItem spot = new OverlayItem(coordinates[i], getAddressFromGP(coordinates[i]), getResources()
                .getString(R.string.timeSinceReported)
                + String.valueOf(timeSinceReported)
                + " "
                + getResources().getString(R.string.minutes));
        malls.addOverlay(spot);
    }
    mapOverlays.add(malls);

where coordinates[i] is the array holding all the markers' coordinates, and getAddressFromGP(coordinates[i]) is a method for reverse GeoCoding using Google API

How can I add any sort of delegate to each overlay? is it possible? I looked at this http://www.javaworld.com/javatips/jw-javatip10.html but couldn't exactly understand if that's what i'm looking for and how to implement it.

Thanks in advance.

in order to notify a class of something that has happened from another class , use the observer design pattern , often called Listener on java since they listen to an event that is supposed to occur.

in order to notify the main thread of something that has occurred from another thread, the most common ways are :

  1. Handler.post
  2. Activity.runOnUiThread
  3. View.post
  4. AsyncTask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM