gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:36:10 25 4
gpt4 key购买 nike

我想刷新我的 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);

}

和主要 Activity

 @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");

}

}

如何纠正这个问题?

最佳答案

改变

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");
}

并确保您在 Poi 对象中实现了 Serialized

关于java - Android Broadcast如何发送和接收对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45128426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com