public class MarkerList{ public MarkerList(int capacity) { super(capacity); } public MarkerList() { } public MarkerList(Collection collection) { super(collection); } ArrayList<String> lat=new ArrayList<>(); ArrayList<String> lng=new ArrayList<>(); ArrayList<String> name=new ArrayList<>(); ArrayList<String> type_2=new ArrayList<>();//Type_2 public int getSize(){ return name.size(); } public ArrayList<String> getLat() { return lat; } public void setLat(ArrayList<String> lat) { this.lat = lat; } public ArrayList<String> getLng() { return lng; } public void setLng(ArrayList<String> lng) { this.lng = lng; } public ArrayList<String> getName() { return name; } public void setName(ArrayList<String> name) { this.name = name; } public ArrayList<String> getType_2() { return type_2; } public void setType_2(ArrayList<String> type_2) { this.type_2 = type_2; } } 

I have this class. I can not transfer via PutExtras. Tell me how to pass my class?

  • one
    pass it on as Parcelable - Vladyslav Matviienko pm
  • @metalurgus, Can you make this class static? not to transmit. And then it's a chore like something through Parcelable. Straight some low-level programming. Outside 2016 - Andro
  • I'm afraid Java is not developing very actively, but this language appeared in the distant 95th year, so calling for 2016 is useless (On the other hand, for example, there is the AutoValue library and the plugin that does the Parcelable class . A static class will not save you during the recovery procedure after complete the application. - xkor
  • one
    If you don’t care about speed (which will be the highest through Parcelable), you can serialize via Serializable, and you don’t need to write anything else, or do it via json serialization, for example Gson ... - Yura Ivanov
  • Clear. Tobish, in any case, you have to pass through the intent ... - Andro

2 answers 2

You need to implement your Parcelable interface near your class Here is an approximate class, with a list of methods and their implementation - edit it for your class by analogy:

 public class Model implements Parcelable { private String title; private String text; /** * Parcel implementation */ private Model(Parcel in) { this.title = in.readString(); this.text = in.readString(); } /** * Parcel implementation */ @Override public int describeContents() { return 0; } /** * Parcel implementation */ @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(title); dest.writeString(text); } /** * Parcel implementation */ public static final Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>() { @Override public Model createFromParcel(Parcel source) { return new Model(source); } @Override public Model[] newArray(int size) { return new Model[size]; } }; } 

Important points:

  1. The class fields in the constructor method ( private Model(Parcel in) ) should be read in the same order in which they are written in the writeToParcel method
  2. It is necessary to read string lists as follows : in.readList(mFotos, String.class.getClassLoader());
  3. How to read / write embedded objects that implement Parcelable is written here: Write a Parcelable to a Parcelable

After that, transfer the object to the Bundle Intent-a as follows:

 Bundle b = new Bundle(); b.putParcelable(someParcelableObject); 

    There is such a wonderful page on which you can easily make Parcelable: http://www.parcelabler.com/

    Here is your Parcelable class:

     public class MarkerList implements Parcelable { public MarkerList(int capacity) { super(capacity); } public MarkerList() { } public MarkerList(Collection collection) { super(collection); } ArrayList<String> lat=new ArrayList<>(); ArrayList<String> lng=new ArrayList<>(); ArrayList<String> name=new ArrayList<>(); ArrayList<String> type_2=new ArrayList<>();//Type_2 public int getSize(){ return name.size(); } public ArrayList<String> getLat() { return lat; } public void setLat(ArrayList<String> lat) { this.lat = lat; } public ArrayList<String> getLng() { return lng; } public void setLng(ArrayList<String> lng) { this.lng = lng; } public ArrayList<String> getName() { return name; } public void setName(ArrayList<String> name) { this.name = name; } public ArrayList<String> getType_2() { return type_2; } public void setType_2(ArrayList<String> type_2) { this.type_2 = type_2; } protected MarkerList(Parcel in) { if (in.readByte() == 0x01) { lat = new ArrayList<String>(); in.readList(lat, String.class.getClassLoader()); } else { lat = null; } if (in.readByte() == 0x01) { lng = new ArrayList<String>(); in.readList(lng, String.class.getClassLoader()); } else { lng = null; } if (in.readByte() == 0x01) { name = new ArrayList<String>(); in.readList(name, String.class.getClassLoader()); } else { name = null; } if (in.readByte() == 0x01) { type_2 = new ArrayList<String>(); in.readList(type_2, String.class.getClassLoader()); } else { type_2 = null; } } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { if (lat == null) { dest.writeByte((byte) (0x00)); } else { dest.writeByte((byte) (0x01)); dest.writeList(lat); } if (lng == null) { dest.writeByte((byte) (0x00)); } else { dest.writeByte((byte) (0x01)); dest.writeList(lng); } if (name == null) { dest.writeByte((byte) (0x00)); } else { dest.writeByte((byte) (0x01)); dest.writeList(name); } if (type_2 == null) { dest.writeByte((byte) (0x00)); } else { dest.writeByte((byte) (0x01)); dest.writeList(type_2); } } @SuppressWarnings("unused") public static final Parcelable.Creator<MarkerList> CREATOR = new Parcelable.Creator<MarkerList>() { @Override public MarkerList createFromParcel(Parcel in) { return new MarkerList(in); } @Override public MarkerList[] newArray(int size) { return new MarkerList[size]; } }; } 
    • Oh cool. ATP ... - Andro
    • This addition to the response of Yuri, the presence of such sites-assistants does not mean that they should be thoughtlessly applied :) - Roman Novoselov