If I call notifyDataSetChanged()
on the custom adapter associated to my ListView, all the views should refresh themself (getView()
will be called).
Now I have a BroadcastReceiver that is listening to an event. When the event fires, the ListView must be refreshed. How can I achieve this?
Thanks!
Asked
Active
Viewed 5,446 times
4

Angelo
- 907
- 3
- 16
- 33
-
"Now I have a BroadcastReceiver that is listening to an event" -- what is the event, and how is the `BroadcastReceiver` registered (manifest or `registerReceiver()`)? – CommonsWare Oct 02 '14 at 14:30
-
Hi, dinamically registered in the Activity that contains the ListView, the event is ACTION_DOWNLOAD_COMPLETE from the DownloadManager. – Angelo Oct 02 '14 at 14:31
-
1in onReceive of BroadcastReceiver implementation you have to: modify underlying data of your custom adapter and call notifyDataSetChanged or create new custom adapter and set it to listview – Selvin Oct 02 '14 at 14:35
-
@Selvin it could be easy if i would have access to the listview from the receiver. I could call getAdapter. I don't know how to access the listview from the receiver (avoiding static or getters) – Angelo Oct 02 '14 at 14:38
-
you said that you have dynamic Reciver from activity... so i think that code looks like http://blog.vogella.com/2011/06/14/android-downloadmanager-example/ then you **have** access to the listview inside onReceive (findViewById(R.id.id_of_listview) should work since this implementation will be not static inner class of Activity) – Selvin Oct 02 '14 at 14:42
-
Add an interface (which is implemented by your activity/fragment). Pass the interface instance, when creating the broadcast receiver, when you receive broadcast (i.e. onReceive()), make a call on the interface, e.g. mDataUpdateListener.onDataAvailable(newData) – Gaurav Arora Oct 02 '14 at 14:43
-
@Selvin it's not like Vogella as the BroadcastReceiver is declared in a separate class file, not inside the Activity. It's like registerReceiver(new DownloadReceiver(), new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); in onCreate() and DownloadReceiver is not declared inside the same Activity – Angelo Oct 02 '14 at 14:48
-
@GauravArora could you write an example as an answer? – Angelo Oct 02 '14 at 14:51
-
will post in a moment. – Gaurav Arora Oct 02 '14 at 15:10
-
Please see the answer section for detailed sample, I havn't compiled or ran this yet, but once you fix any required initialization and correct assumption, the solution should work well. – Gaurav Arora Oct 02 '14 at 15:17
4 Answers
2
If you refresh listview from receiver you'll have code like this:
BroadcastReceiver br;
public final static String BROADCAST_ACTION = "BROADCAST_ACTION";
br = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//code refreshing...
}
};
IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
registerReceiver(br, intFilt);
And you call it with code:
Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);
If you need the refresh to be another action you just need to add (after action):
Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);

QArea
- 4,955
- 1
- 12
- 22
-
-
I think the "//code refreshing..." part is the hard part here. – Stephen M -on strike- Jul 17 '18 at 22:44
1
As requested, please see the sample code below:
public interface OnDataUpdateListener {
void onDataAvailable(ArrayList<String> newDataList);
}
public class MyTestReceiver extends BroadcastReceiver {
public static final String DATA_LIST = "DATA_LIST";
private OnDataUpdateListener mDataUpdateListener = null;
public MyTestReceiver(OnDataUpdateListener dataUpdateListener) {
mDataUpdateListener = dataUpdateListener;
}
@Override
public void onReceive(Context ctx, Intent intent) {
// assuming data is available in the delivered intent
ArrayList<String> dataList = intent.getSerializableExtra(DATA_LIST);
if (null != mDataUpdateListener) {
mDataUpdateListener.onDataAvailable(dataList);
}
}
}
public class MyActivity extends FragmentActivity implements OnDataUpdateListener {
public static final String ACTION_DATA_UPDATE_READY = "ACTION_DATA_UPDATE_READY";
private MyTestReceiver mTestReceiver = null;
private <SomeAdapterClass> mAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// other required initialization
mTestReceiver = new MyTestReceiver(this);
}
@Override
public void onResume() {
super.onResume();
if (null != mTestReceiver) {
registerReceiver(mTestReceiver, new IntentFilter(ACTION_DATA_UPDATE_READY));
}
}
void onDataAvailable(ArrayList<String> newDataList) {
// assuming you want to replace existing data and not willing to append to existing dataset
mAdapter.clear();
mAdapter.addAll(newDataList);
mAdapter.notifyDataSetChanged();
}
}

Gaurav Arora
- 1,805
- 13
- 13
0
In the code where your data is updated, fire off a message signalling that data has been changed... (You will need access to either the Activity or the Application context to do this)
Intent intent = new Intent("ListViewDataUpdated");
LocalBroadcastManager.getInstance(context.sendBroadcast(intent));
Then just catch the catch the message using the following code in your activity, and tell your ListAdapter to update...
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myListAdapter.notifyDataSetChanged();
}
};
@Override
public void onResume(){
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("ListViewDataUpdated"));
myListAdapter.notifyDataSetChanged();//in case our data was updated while this activity was paused
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
Credit: adapted from Vogella

Mtl Dev
- 1,604
- 20
- 29
0
LocalBroadcastManager.getInstance(context.sendBroadcast(intent));
change to
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
I might be wrong but it works for me...

Marinus Snyman
- 55
- 4