Android Broadcast如何发送和接收对象列表

时间:2017-07-16 12:09:53

标签: java android list

我想刷新我的ListView。在我的服务中,我准备了对象列表。如何在ListView中发送对象列表并接收?

我的服务

 @Override
    public void onLocationChanged(Location location) {
    //populate list
    //send list
    sendLocationBroadcast(poiList);
}
 private void sendLocationBroadcast(List<Poi> poiList) {

        Intent locationIntent = new Intent();
        locationIntent.setAction(LOACTION_ACTION);
        locationIntent.putExtra(LOCATION_MESSAGE, poiList.toString());

        LocalBroadcastManager.getInstance(this).sendBroadcast(locationIntent);

}

主要活动

 @Override
        public void onReceive(Context context, Intent intent) {


            if (null != intent && intent.getAction().equals(LOACTION_ACTION)) {

                String locationData = intent.getStringExtra(LOCATION_MESSAGE);

                ArrayList<Poi> dataList = (ArrayList<Poi>) intent.getSerializableExtra("DATA_LIST");

            }

        }

如何纠正?

1 个答案:

答案 0 :(得分:1)

更改

locationIntent.putExtra(LOCATION_MESSAGE, poiList.toString());

Bundle bundle = new Bundle();
bundle.putSerializable("data",poiList);
locationIntent.putExtras(bundle);

读取数据

if (getIntent().hasExtra("data")) {
    Bundle bundle = getIntent().getExtras();
    if(bundle!=null)
         ArrayList<Poi> dataList = (ArrayList<Venue>) bundle.getSerializable("arrayListVenue");
}

并确保implements Serializable对象<{p>}上的Poi