简体   繁体   English

如何将包含可包裹类“ B”的实例的可包裹类“ A”的实例写入类“ B”的包裹

[英]How to write an instance of Parcelable class 'A' which contains an instance of parcelable class 'B', to Parcel from class 'B'

I have some problems with using parcelables for my Android app. 我在Android应用程序中使用可包裹货物时遇到一些问题。
I have a custom class named Transport , which contains a bunch of RoutePoint -instances which has been loaded from a file and saved in an array. 我有一个名为Transport的自定义类,其中包含一堆RoutePoint ,这些RoutePoint已从文件加载并保存在数组中。

Both the classes implements Parcelable , because I want to pass the Transport object, which contains RoutePoint objects, between multiple activities. 这两个类都implements Parcelable ,因为我想在多个活动之间传递包含RoutePoint对象的Transport对象。

This brings a problem, because inside of each RoutePoint object, I have a Transport object used to tell which transport is the parent for the RoutePoint . 这就带来了一个问题,因为在每个RoutePoint对象的内部,都有一个Transport对象,该对象用于告诉哪个传输是RoutePoint的父RoutePoint

Pretend that I have these classes: 假设我有这些课程:

Transport : Transport

public class Transport implements Parcelable
{
    private RoutePoint[] points;

    public Transport()
    {
        // Here the array is initialized
    }

    // Methods needed because of the Parcelable interface
}

RoutePoint : RoutePoint

public class RoutePoint implements Parcelable
{
    private Transport parent;

    public RoutePoint(Transport parent)
    {
        this.parent = parent;
    }

    // Methods needed because of the Parcelable interface
}

When the Transport class is written to a Parcel , I write all the RoutePoint s from the array to the parcel too. 当将Transport类写入Parcel ,我也将数组中的所有RoutePoint都写入了包裹。

When the RoutePoint class is written to a Parcel , I write the parent Transport to the parcel too. RoutePoint类被写入到一个Parcel ,我写的父Transport的包裹了。

This causes a "recursive call", and a StackOverflowException : 这将导致“递归调用”和StackOverflowException

  1. The Transport writes a RoutePoint to the Parcel . TransportRoutePoint写入Parcel
  2. The RoutePoint writes the parent Transport to the Parcel . RoutePoint写入父TransportParcel
  3. The parent Transport writes a RoutePoint to the Parcel . TransportRoutePoint写入Parcel
  4. Back to step 2. 返回步骤2。

I wonder how I can get around this problem, and pass the Transport class between activities, and passing the parent object in the RoutePoint class in some way. 我想知道如何解决此问题,并在活动之间传递Transport类,并以某种方式在RoutePoint类中传递parent对象。

If all RoutePoint instances are always contained inside a Transport instance, you can fix it by not serializing RoutePoint.parent, but rather setting it when you deserialize Transport. 如果所有RoutePoint实例始终包含在Transport实例内,则可以通过不序列化RoutePoint.parent而是在反序列化Transport时进行设置来对其进行修复。

So, when you deserialize a Transport instance, deserialize the RoutePoints, then call a setTransport method on those RoutePoint instances and set the parent. 因此,在反序列化Transport实例时,反序列化RoutePoints,然后在这些RoutePoint实例上调用setTransport方法并设置父对象。

Having Circular reference like that is not really a good idea from GC perspective too. 从GC的角度来看,具有这样的循环引用也不是一个好主意。 I would suggest you have one way reference and use other data structure like BiMap etc to have those relationship defined. 我建议您使用一种方法引用,并使用其他数据结构(如BiMap等)来定义这些关系。 One way is to create an Empty constructor in RoutePoint and only parcel other item except for the parent. 一种方法是在RoutePoint中创建一个Empty构造函数,然后仅打包除父项以外的其他项。 In Transport, while reading from parcel, set its reference later. 在运输中,从包裹中读取时,请稍后设置其参考。 Hope this makes sense. 希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何传递包含另一个可包裹 class 列表的可包裹 class? - How to pass a Parcelable class which contains a list of another parcelable class? 如何在 Java 中的 Parcelable 类中打包对象类型 - how to parcel Object type in Parcelable class in Java Parcelable类中的Parcelable列表不使用Parcel.readTypedList进行读回 - Parcelable list inside Parcelable class not reading back with Parcel.readTypedList 如何在Parcelable类中实现读写 - How to implement the write & read in Parcelable Class 如何使内部类Parcelable - How to make an inner class Parcelable 可以从在A类实例中创建的另一个B类实例访问A类实例的字段 - is possible to access to the field of a A class instance from another B class instance created in the A class instance 可以使用类A的实例作为类B的属性吗 - Is it okay to use an instance of class A as a attribute of class B 如果类B的实例是类A的成员,那么当按下类B中的按钮时,类B如何调用类A的方法? - If an instance of class B is a member of class A, how can class B call a method of class A when a button in class B is pressed? 如何通过Class B中的互变量类型访问Class A的实例 - How to access an instance of Class A by a mutual variable type in Class B 抽象类作为parcelable - Abstract class as parcelable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM