Chris
Chris

Reputation: 4364

Update Adapter data from an external BroadcastReceiver

I have a BroadcastReceiver which is used for different alarms set by the AlarmManger. This Receiver will check if an event is fulfilled when it starts (depending on the location).

These events are displayed via a BaseExpandableListAdapter. What is the easiest way to update the events in my adapter from the Receiver? The receiver cannot be an inner class! It is used in different Activities and Fragments!

What I tried was to pass an callback to my Adapter to notify the Activity/Fragment to update the data. "Events before" and "Events after" are always 0 (I think this is due to the getInstance()-method fails somehow).

public class EventAdapter(Context context, List<Event> events, EventAdapterCallback callback) {
    super();
    this.context = context;
    this.events = events;
    this.callback = callback;

    instance = this;
}

public static EventAdapter getInstance() {
    return instance;
}

public void requestUpdate() {
    callback.update();
}


public class EventBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        // ... doing my stuff
        // send live update to the adapter
        EventAdapter.getInstance().requestUpdate();
    }
}


public class RandomFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        eventsAdapter = new EventAdapter(getActivity(), events, new EventAdapterCallback() {
            public void update() {
                Log.i("Events before:", String.valueOf(events.size()));
                events.clear();
                events.addAll(database.getList());
                Log.i("Events after:", String.valueOf(events.size()));
                eventsAdapter.notifyDataSetChanged();
            }
        });
    }
}

Upvotes: 0

Views: 1604

Answers (2)

Vino
Vino

Reputation: 1544

Perhaps you are doing this somewhere in the code. But I could not locate it.

You need to register the broadcast receiver in your fragment.

@Override
public void onResume()
{
    super.onResume();
    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance (this);

    localBroadcastManager.registerReceiver (<instance-of-your-broadcast-receiver>, new IntentFilter ("UpdateRandomFragment"));
}

@Override
public void onPause()
{
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver (<instance-of-your-broadcast-receiver>);
}

In your onReceive

    @Override
    public void onReceive(final Context context, Intent intent) 
    {
        String action = intent.getAction();
        if (action.equals ("UpdateRandomFragment"))
        {
            ((RandomFragment)context).eventAdapter.clear();
            ((RandomFragment)context).eventAdapter.addAll(...);
            ((RandomFragment)context).eventAdapter.notifyDataSetChanged();
        }
    }

Upvotes: 1

elmorabea
elmorabea

Reputation: 3263

Use observer pattern, make all your adapters implement an interface let's say Updatable, containing an update() method.

Have any manager "singleton" or your app Application class, have a List any in all your activities or fragments that has an adapter that needs to be updated, add that adapter to the list, at the destruction of the activity or fragment, remove the adapter from the list.

when ever you have something you need to update the adapters with, loop over the list and call their update method.

Upvotes: 1

Related Questions