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:

  • Android Network Service Discovery “timeout”?
  • Setting Roboto font in TextView - xml
  • How do I connect Android apps with Google Sheets spreadsheets?
  • How to communicate App with Server without internet connection?
  • Colored boxed with letters a la Gmail
  • Using GoogleApiClient in a service
  • 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?

    Related posts:

    How to show an icon in the status bar when application is running, including in the background?
    Android SQLite: Which query (&ldquo;query&rdquo; or &ldquo;rawQuery&rdquo;) is faste...
    Identifying refunded Play Store downloads
    onIabPurchaseFinished never called.
    How to find the status of VPN connection through framework APIs or any other efficient method?
    Android project build successfully but ran failed with error: java.util.zip.ZipException: duplicate ...
  • Android Simple TextView Animation
  • What is the right screen size and density configuration of Nexus 6?
  • Different fling (swipe) velocity on different Android devices with same density
  • Android - RecyclerView spacing between items in a Grid
  • Call requires API level 16 (current min is 14): android.app.Notification.Builder#build
  • sliding one fragment over another in Android
  • 4 Solutions collect form web for “how to properly implement Parcelable with an ArrayList<Parcelable>?”

    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.

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

    _devices = in.createTypedArrayList(ZigBeeDev.CREATOR);
    

    You should use writeList(List l) for your list of integers and writeTypedList(List val) for the list of ZigBeeDevices

    In constructor you should use

    _lqis = in.createTypedArrayList(ZigBeeDev.CREATOR);
    

    And in “writeToParcel” use

    out.writeTypedList(_lqis);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.