如何发送自定义对象的ArrayList中包 [英] How to send an ArrayList of custom objects in a Bundle

查看:149
本文介绍了如何发送自定义对象的ArrayList中包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一个服务来创建自定义对象(MyObject来)每x秒的一个ArrayList的应用程序。然后,我希望我的活动获得此ArrayList

I have an application that uses a service to create an ArrayList of custom objects (MyObject) every x seconds. I then want my Activity to obtain this ArrayList.

我目前打算让服务每次完成查询的数据时,将消息发送到活动的处理程序。我希望消息处理程序包含MyObjects的ArrayList的。

I'm currently planning on having the Service send a message to the Activity's handler every time it finishes the query for the data. I want the message to the Handler to contain the ArrayList of MyObjects.

在建设活动中​​的方法获取此ArrayList出来的消息,我发现我不能。

When building the method in the Activity to get this ArrayList out of the message, I noticed that I couldn't.

如果我试图

msg.getData().getParcelableArrayList("myObjects")

然后,我将其传递给该方法需要一个ArrayList不会接受它。如果我试图铸造的结果:

Then the method I was passing it to that expected an ArrayList wouldn't accept it. If I tried casting the results:

(ArrayList<MyObject>)msg.getData().getParcelableArrayList("myObjects")

我收到了错误:无法从ArrayList中&LT投; Parcelable&GT;到ArrayList的&LT;为MyObject&GT;

MyObject来实现Parcelable,我已经成功地通过让我的活动呼叫服务的方法来检索它发送一个ArrayList,从我的服务,我的活动。我试图从有我的活动民意调查我的服务针对这一数据,虽然走了。

MyObject implements Parcelable and I have successfully sent an ArrayList from my service to my activity by having my activity call a method on the service to retrieve it. I'm trying to go away from having my activity poll my service for this data, though.

1)如何发送包里面一个ArrayList的消息处理程序?

2)是否有我应该使用有我的服务更新在我的活动数据,可能会或可能无法看到一个不同的模型?我总是想在我的活动是从服务的最新数据。

推荐答案

还有另一种模式,应该使用。还有一个问题,我问提供了答案:

There is another model that should be used. Another question I asked provided the answer:

<一个href="http://stackoverflow.com/questions/3731260/sup$p$pss-notifications-from-a-service-if-activity-is-running">http://stackoverflow.com/questions/3731260/sup$p$pss-notifications-from-a-service-if-activity-is-running

至于#1,你可以解决它通过从ArrayList的声明仅仅取消了仿制药,并在需要的地方浇注适当。我知道这工作,因为这是基于问其他问题重构前我做了什么。

As for #1, you could get around it by just removing the generics from the ArrayList declarations and casting as appropriate where needed. I know this works because that's what I did before refactoring based on the other question asked.

这篇关于如何发送自定义对象的ArrayList中包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆