2017-07-16 40 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對象