3

how can we parcel nested object in an intent?.
For example lets say there is an object A which contain one string variable and one another object B. now B contain object C . C contain a list of string. so how i can parcel object A in an intent.
any help on this will be appreciated.
thanks in advance.

Implementation:

public static class A 
{
    private B  ObjectB;
}

public static class B 
{
    private String type;
    private List<C> C;
}

public static class C 
{
    private List<D> D;
}

public static class D 
{
    private String id;
    private String name;
    private String address;
    private String email;
}

how to write parcelable for class C. i am using

dest.writeParceable(ObjectC,flag)

For reading:

in.readParcelable(C.getClass().getClassLoader());

but its not working

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82

2 Answers2

1

You would have to implement parcelable for object B and then implement parcelable for object A. I have never done this but the above should work.

Edit

See the code snippets below which illustrates how to implement parcelable for Class C.

  1. First implement parcelable for Class D like so:

    dest.writeString(id); 
    dest.writeString(name);  
    dest.writeString(address);  
    dest.writeString(email);
    
    id = in.readString();  
    name = in.readString();  
    address = in.readString();  
    email = in.readString();  
    
  2. Then implement parcelable for Class C as follows:

    dest.writeList(D);
    
    in.readList(D,this.getClass().getClassLoader());
    

This is untested code as I have never implemented nested parceling but worth a try. Hope that helps.

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
Mandel
  • 2,968
  • 2
  • 22
  • 19
  • thanks mandel.it should work . but i dont know how to implement parcelable for object B. what should we write in writeToParcel(Parcel dest, int flags) and readFromParcel method when we implement parcelable for Object B. –  Jul 10 '11 at 18:14
  • You will need to add code like so: `dest.writeString(.);` in writeToParce(.) and like so: `testString = in.readString();` in readFromParcel(.). Specifics will depend on the classes involved. – Mandel Jul 10 '11 at 18:23
  • No Mandel, it will not work for object. B is a collection of custom objects. writeString will work for string only. –  Jul 10 '11 at 18:35
  • I just gave an example. Show me the three classes and I will be more specific. – Mandel Jul 10 '11 at 18:40
  • public static class DiagnosticHelp { private List categories; –  Jul 11 '11 at 06:46
  • and if class A would be like this:
    public static class A { private B <B> objectB;
    }
    then how will we implement parcel for b.
    –  Jul 12 '11 at 08:08
  • how to write parcelable for class A? –  Jul 12 '11 at 08:14
0

One way I have seen this done is using serializeable

eg.

if you have a variable:

Date createdAt;

Then in writeToParcel(Parcel out, int flags) you can write:

out.writeSerializable(createdAt); 

To read the value back yoy would use:

createdAt = (Date) in.readSerializable();

That being said though Parcelable was specifically created because Java serialization was considered too slow. So while this works it is not a good idea to use for larger pieces of data.

JFreeman
  • 974
  • 1
  • 10
  • 26
Ian
  • 147
  • 2
  • 9