2017-07-16 117 views
0

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

我的服务

@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

变化

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 SerializablePoi对象