Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to properly implement Parcelable with an ArrayList<Parcelable>?

I am having trouble making my class Parcelable. The trouble is, I am trying to write to the parcel a member in the class which is an ArrayList<Parcelable> object. The ArrayList is Serializable, and the objects (ZigBeeDev) in the list are Parcelable.

Here is the relevant code:

package com.gnychis.coexisyst;  import java.util.ArrayList; import java.util.Iterator;  import android.os.Parcel; import android.os.Parcelable;  public class ZigBeeNetwork implements Parcelable {      public String _mac;             // the source address (of the coordinator?)     public String _pan;             // the network address     public int _band;               // the channel     ArrayList<Integer> _lqis;       // link quality indicators (to all devices?)     ArrayList<ZigBeeDev> _devices;  // the devices in the network      public void writeToParcel(Parcel out, int flags) {         out.writeString(_mac);         out.writeString(_pan);         out.writeInt(_band);         out.writeSerializable(_lqis);         out.writeParcelable(_devices, 0);  // help here     }      private ZigBeeNetwork(Parcel in) {         _mac = in.readString();         _pan = in.readString();         _band = in.readInt();         _lqis = (ArrayList<Integer>) in.readSerializable();         _devices = in.readParcelable(ZigBeeDev.class.getClassLoader());  // help here     }      public int describeContents() {         return this.hashCode();     }      public static final Parcelable.Creator<ZigBeeNetwork> CREATOR =              new Parcelable.Creator<ZigBeeNetwork>() {         public ZigBeeNetwork createFromParcel(Parcel in) {             return new ZigBeeNetwork(in);         }          public ZigBeeNetwork[] newArray(int size) {             return new ZigBeeNetwork[size];         }     };      //... } 

I have marked two spots "// help here" to understand how to properly write to the parcel and how to reconstruct it. If ZigBeeDev is Parcelable (properly tested), how do I do this properly?

like image 256
gnychis Avatar asked Aug 12 '11 15:08

gnychis


People also ask

How do you implement Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

What is a Parcelable object?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

What is a primary purpose of the Parcelable interface?

The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another.

Are bundles Parcelable?

Bundle is a container for named values of types standard for android (including Parcelable ), which is used to pass data between activies, fragments and other android app entites.


2 Answers

You almost got it !

You just need to do :

public void writeToParcel(Parcel out, int flags) {     out.writeString(_mac);     out.writeString(_pan);     out.writeInt(_band);     out.writeSerializable(_lqis);     out.writeTypedList(_devices); }  private ZigBeeNetwork(Parcel in) {     _mac = in.readString();     _pan = in.readString();     _band = in.readInt();     _lqis = (ArrayList<Integer>) in.readSerializable();     in.readTypedList(_devices, ZigBeeDev.CREATOR); } 

That's all!

For your list of Integer, you can also do :

out.writeList(_lqis); _lqis = new ArrayList<>(); in.readList(_lqis Integer.class.getClassLoader()); 

It should work.

like image 91
NitroG42 Avatar answered Sep 19 '22 15:09

NitroG42


In my case in.readTypedList(_devices, ZigBeeDev.CREATOR); gave me a NullPointerException on _devices. So I used this:

_devices = in.createTypedArrayList(ZigBeeDev.CREATOR); 
like image 31
osrl Avatar answered Sep 20 '22 15:09

osrl