How to update ListView from BroadcastReceiver?

If I call notifyDataSetChanged()to the user adapter associated with my ListView, all views must be updated themselves ( getView()will be called). Now I have a BroadcastReceiver that listens to the event. When the event fires, the ListView needs to be updated. How can i achieve this? Thank!

+4
source share
3 answers

If you update the list from the recipient, you will have the following code:

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 code:

Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);

If you need to update to be another action, you just need to add (after the action):

Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);
+2
source

. :

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();
    }
}
+1

In the code in which your data is updated, turn off the message that the data has been changed ... (For this you need access to the Activity or Application context)

Intent intent = new Intent("ListViewDataUpdated");
LocalBroadcastManager.getInstance(context.sendBroadcast(intent));

Then just catch the message using the following code in your activity and let the updated ListAdapter list ...

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

0
source

All Articles