如何发送 Bundle 中的自定义对象的 ArrayList

发布于 2024-09-19 06:32:42 字数 858 浏览 5 评论 0原文

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

我目前计划让服务在每次完成数据查询时向活动处理程序发送一条消息。我希望发送给处理程序的消息包含 MyObjects 的 ArrayList。

当在 Activity 中构建方法以从消息中获取此 ArrayList 时,我注意到我做不到。

如果我尝试

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

那么我将其传递给预期 ArrayList 的方法将不会接受它。如果我尝试转换结果:

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

我收到错误:Cannot cast from ArrayList; to ArrayList

MyObject 实现了 Parcelable,并且我已通过让我的活动调用服务上的方法来检索它,成功地将 ArrayList 从我的服务发送到我的活动。不过,我试图避免让我的活动轮询我的服务来获取这些数据。

1) 如何将捆绑包内的 ArrayList 通过消息发送给处理程序?

2) 我应该使用不同的模型来让我的服务更新 Activity 中的数据,这可能会导致或者可能不可见?我始终希望我的活动中的数据是来自服务的最新数据。

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.

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.

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

If I tried

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

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

I received the error: Cannot cast from ArrayList<Parcelable> to ArrayList<MyObject>

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) How can I send an ArrayList inside the bundle in a message to handler?

2) Is there a different model I should be using to have my service update the data in my Activity that may or may not be visible? I always want the data in my activity to be the latest from the Service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡墨 2024-09-26 06:32:42

我有完全相同的问题,虽然仍在与 Parcelable 争论不休,但我发现静态变量对于该任务来说并不是一个坏主意。

您可以简单地创建一个静态字段

public static ArrayList<MyObject> myObjects = .. 

并通过 MyRefActivity.myObjects 从其他地方使用它。

我不确定公共静态变量在具有活动的应用程序上下文中意味着什么。如果您对此方法或此方法的性能方面也有疑问,请参阅:

干杯。

I had the exact same question and while still hassling with the Parcelable, I found out that the static variables are not such a bad idea for the task.

You can simply create a static field

public static ArrayList<MyObject> myObjects = .. 

and use it from elsewhere via MyRefActivity.myObjects

I was not sure about what public static variables imply in the context of an application with activities. If you also have doubts about either this or on performance aspects of this approach, refer to:

Cheers.

难以启齿的温柔 2024-09-26 06:32:42

应该使用另一种模型。我问的另一个问题提供了答案:

抑制来自服务的通知,如果 。

对于#1,您可以通过从 ArrayList 声明中删除泛型并根据需要进行适当的转换来解决它 我知道这是有效的,因为这就是我在根据提出的其他问题进行重构之前所做的。

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

Suppress notifications from a service if activity is running

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.

月下凄凉 2024-09-26 06:32:42

如果演员阵容有问题,就保留它,不要演员阵容,错误就会消失。

If the cast is the problem, just leave it be, dont cast it, the error will go away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文