1

My app is supposed to track airplanes on a map using data streamed over the Internet. I use a Service to receive the data, and then I use Messengers to send messages between it and my MainActivity (thanks to this guide), which has my Fragment with a SupportMapFragment (MainMapFragment) and also a RecyclerView Fragment (AircraftListFragment).

Each Marker is in a HashMap with an Aircraft object in MainMapFragment, and the Aircraft objects are maintained by the MainActivity. Is there anything like notifyDatasetChanged() for my Activity to update my MapFragment? Obviously I can't write a method in MainMapFragment like this:

public void updateAircraftArrayList(ArrayList<Aircraft> updatedAircrafts){
    this.aircraftArrayList = updatedAircrafts;
    updateMarkers(); //Update the Markers on the map
}

accessed like this in my MainActivity:

class IncomingHandler extends android.os.Handler {
    @Override
    public void handleMessage(Message msg){
        String aircraftMsg = msg.getData().getString("fromSocket");
        Aircraft newAircraft = decodeSbsMessage(aircraftMsg);
        aircraftArrayList = searchThroughArrayListForAircraft(newAircraft);
        MainMapFragment.updateAircraftArrayList(aircraftArrayList);
    }
}

I've heard of EventBus, but from what I've heard it's not very good for handling a constant stream from a Socket.

Community
  • 1
  • 1
CiaranC94
  • 176
  • 4
  • 20

1 Answers1

1

You can call updateAircraftArrayList method from your MainActivity:

your_instance_of_MainMapFragment.updateAircraftArrayList(aircraftArrayList)
ozo
  • 763
  • 7
  • 13
  • I'm laughing at myself right now for missing this, this problem was racking my brain for the last few days. Thanks for your help! – CiaranC94 Mar 27 '16 at 15:01
  • If it solved your problem, you may accept it as an answer. – ozo Mar 27 '16 at 16:50