0

i send my pojo object using intent from one class to another

its successful send and successful retrieve from another class

but when i check inner data from retrive pojo my inner arraylist name links and phone returns null; i dont know why because my other data name and data are filled but arraylist not fill

this i retrive data using intent

this i send data using intent

my pojo class is

public class Datum implements Parcelable {

@SerializedName("assigned_to")
private String mAssignedTo;
@SerializedName("contact_id")
private String mContactId;
@SerializedName("created_date")
private String mCreatedDate;
@SerializedName("first_name")
private String mFirstName;
@SerializedName("img")
private String mImg;
@SerializedName("last_name")
private String mLastName;
@SerializedName("links")
private List<Link> mLinks;
@SerializedName("modified_date")
private String mModifiedDate;
@SerializedName("phone")
private List<Phone> mPhone;
@SerializedName("photo")
private String mPhoto;
@SerializedName("salutation")
private String mSalutation;
@SerializedName("subscription_id")
private String mSubscriptionId;
@SerializedName("type")
private String mType;
private boolean isChecked;

protected Datum(Parcel in) {
    mAssignedTo = in.readString();
    mContactId = in.readString();
    mCreatedDate = in.readString();
    mFirstName = in.readString();
    mImg = in.readString();
    mLastName = in.readString();
    mModifiedDate = in.readString();
    mPhoto = in.readString();
    mSalutation = in.readString();
    mSubscriptionId = in.readString();
    mType = in.readString();
    isChecked = in.readByte() != 0;
}

public static final Creator<Datum> CREATOR = new Creator<Datum>() {
    @Override
    public Datum createFromParcel(Parcel in) {
        return new Datum(in);
    }

    @Override
    public Datum[] newArray(int size) {
        return new Datum[size];
    }
};

public boolean isChecked() {
    return isChecked;
}

public void setChecked(boolean checked) {
    isChecked = checked;
}

public String getAssignedTo() {
    return mAssignedTo;
}

public void setAssignedTo(String assignedTo) {
    mAssignedTo = assignedTo;
}

public String getContactId() {
    return mContactId;
}

public void setContactId(String contactId) {
    mContactId = contactId;
}

public String getCreatedDate() {
    return mCreatedDate;
}

public void setCreatedDate(String createdDate) {
    mCreatedDate = createdDate;
}

public String getFirstName() {
    return mFirstName;
}

public void setFirstName(String firstName) {
    mFirstName = firstName;
}

public String getImg() {
    return mImg;
}

public void setImg(String img) {
    mImg = img;
}

public String getLastName() {
    return mLastName;
}

public void setLastName(String lastName) {
    mLastName = lastName;
}

public List<Link> getLinks() {
    return mLinks;
}

public void setLinks(List<Link> links) {
    mLinks = links;
}

public String getModifiedDate() {
    return mModifiedDate;
}

public void setModifiedDate(String modifiedDate) {
    mModifiedDate = modifiedDate;
}

public List<Phone> getPhone() {
    return mPhone;
}

public void setPhone(List<Phone> phone) {
    mPhone = phone;
}

public String getPhoto() {
    return mPhoto;
}

public void setPhoto(String photo) {
    mPhoto = photo;
}

public String getSalutation() {
    return mSalutation;
}

public void setSalutation(String salutation) {
    mSalutation = salutation;
}

public String getSubscriptionId() {
    return mSubscriptionId;
}

public void setSubscriptionId(String subscriptionId) {
    mSubscriptionId = subscriptionId;
}

public String getType() {
    return mType;
}

public void setType(String type) {
    mType = type;
}



@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mAssignedTo);
    dest.writeString(mContactId);
    dest.writeString(mCreatedDate);
    dest.writeString(mFirstName);
    dest.writeString(mImg);
    dest.writeString(mLastName);
    dest.writeString(mModifiedDate);
    dest.writeString(mPhoto);
    dest.writeString(mSalutation);
    dest.writeString(mSubscriptionId);
    dest.writeString(mType);
    dest.writeByte((byte) (isChecked ? 1 : 0));
}
}

this is my links pojo

public class Link implements Parcelable {

@SerializedName("company")
private String mCompany;
@SerializedName("company_id")
private String mCompanyId;
@SerializedName("edit")
private String mEdit;
@SerializedName("id")
private String mId;
@SerializedName("img")
private String mImg;
@SerializedName("logo")
private String mLogo;
@SerializedName("record_type")
private String mRecordType;
@SerializedName("relation")
private String mRelation;

protected Link(Parcel in) {
    mCompany = in.readString();
    mCompanyId = in.readString();
    mEdit = in.readString();
    mId = in.readString();
    mImg = in.readString();
    mLogo = in.readString();
    mRecordType = in.readString();
    mRelation = in.readString();
}

public static final Creator<Link> CREATOR = new Creator<Link>() {
    @Override
    public Link createFromParcel(Parcel in) {
        return new Link(in);
    }

    @Override
    public Link[] newArray(int size) {
        return new Link[size];
    }
};

public String getCompany() {
    return mCompany;
}

public void setCompany(String company) {
    mCompany = company;
}

public String getCompanyId() {
    return mCompanyId;
}

public void setCompanyId(String companyId) {
    mCompanyId = companyId;
}

public String getEdit() {
    return mEdit;
}

public void setEdit(String edit) {
    mEdit = edit;
}

public String getId() {
    return mId;
}

public void setId(String id) {
    mId = id;
}

public String getImg() {
    return mImg;
}

public void setImg(String img) {
    mImg = img;
}

public String getLogo() {
    return mLogo;
}

public void setLogo(String logo) {
    mLogo = logo;
}

public String getRecordType() {
    return mRecordType;
}

public void setRecordType(String recordType) {
    mRecordType = recordType;
}

public String getRelation() {
    return mRelation;
}

public void setRelation(String relation) {
    mRelation = relation;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mCompany);
    dest.writeString(mCompanyId);
    dest.writeString(mEdit);
    dest.writeString(mId);
    dest.writeString(mImg);
    dest.writeString(mLogo);
    dest.writeString(mRecordType);
    dest.writeString(mRelation);
}

}

this is number pojo

public class Phone implements Parcelable{

@SerializedName("flagecode")
private String mFlagecode;
@SerializedName("id")
private String mId;
@SerializedName("is_primary")
private String mIsPrimary;
@SerializedName("phone_type")
private String mPhoneType;
@SerializedName("phone_type_id")
private String mPhoneTypeId;
@SerializedName("value")
private String mValue;

protected Phone(Parcel in) {
    mFlagecode = in.readString();
    mId = in.readString();
    mIsPrimary = in.readString();
    mPhoneType = in.readString();
    mPhoneTypeId = in.readString();
    mValue = in.readString();
}

public static final Creator<Phone> CREATOR = new Creator<Phone>() {
    @Override
    public Phone createFromParcel(Parcel in) {
        return new Phone(in);
    }

    @Override
    public Phone[] newArray(int size) {
        return new Phone[size];
    }
};

public String getFlagecode() {
    return mFlagecode;
}

public void setFlagecode(String flagecode) {
    mFlagecode = flagecode;
}

public String getId() {
    return mId;
}

public void setId(String id) {
    mId = id;
}

public String getIsPrimary() {
    return mIsPrimary;
}

public void setIsPrimary(String isPrimary) {
    mIsPrimary = isPrimary;
}

public String getPhoneType() {
    return mPhoneType;
}

public void setPhoneType(String phoneType) {
    mPhoneType = phoneType;
}

public String getPhoneTypeId() {
    return mPhoneTypeId;
}

public void setPhoneTypeId(String phoneTypeId) {
    mPhoneTypeId = phoneTypeId;
}

public String getValue() {
    return mValue;
}

public void setValue(String value) {
    mValue = value;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mFlagecode);
    dest.writeString(mId);
    dest.writeString(mIsPrimary);
    dest.writeString(mPhoneType);
    dest.writeString(mPhoneTypeId);
    dest.writeString(mValue);
}

}

i send my data

Intent iEditContact = new Intent(JsonParseActivity.this, EditContatctActivity.class);
                iEditContact.putExtra(Constant.intent_key_edit_contact, contact);
                startActivityForResult(iEditContact, Constant.edt_contect_request_code);

and received with

if (iContact != null && iContact.hasExtra(Constant.intent_key_edit_contact)) {
            contact = iContact.getParcelableExtra(Constant.intent_key_edit_contact);
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
BiRjU
  • 733
  • 6
  • 23

3 Answers3

0

You have to initialize mLinks in both writeToParcel(Parcel dest, int flags) and Phone(Parcel in)

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mCompany);
dest.writeString(mCompanyId);
dest.writeString(mEdit);
dest.writeTypedList(mLinks );
dest.writeString(mId);
dest.writeString(mImg);
dest.writeString(mLogo);
dest.writeString(mRecordType);
dest.writeString(mRelation);
 }

And also in Constructor

protected Phone(Parcel in) {
mFlagecode = in.readString();
mId = in.readString();
mLinks=new List<>();
in.readTypedList(mLinks, Phone.CREATOR);
mIsPrimary = in.readString();
mPhoneType = in.readString();
mPhoneTypeId = in.readString();
mValue = in.readString();

}

This should work in your case

Akshay
  • 318
  • 1
  • 15
  • i write in Datum.java in @Override public void writeToParcel(Parcel dest, int flags) {dest.writeTypedList(mLinks); dest.writeTypedList(mPhone);} and also mLinks = new ArrayList<>(); in.readTypedList(mLinks, Link.CREATOR); mPhone = new ArrayList<>(); in.readTypedList(mPhone, Phone.CREATOR); in protected Datum(Parcel in) { mLinks = new ArrayList<>(); in.readTypedList(mLinks, Link.CREATOR); mPhone = new ArrayList<>(); in.readTypedList(mPhone, Phone.CREATOR); but not worked – BiRjU Mar 15 '18 at 09:05
  • Clean and build your project or else delete the existing project and create it from scratch, sometimes Parcelable will create problems if you edit or add some elements – Akshay Mar 15 '18 at 09:28
0

Your Datum Model class missing writeList() and Read. That's why those fields are null. Your methods should look like below.

 protected Datum(Parcel in) {
    this.mContactId = in.readString();
    this.mAssignedTo = in.readString();
    this.mCreatedDate = in.readString();
    this.mFirstName = in.readString();
    this.mImg = in.readString();
    this.mLastName = in.readString();
    this.mLinks = new ArrayList<Link>();
    in.readList(this.mLinks, Link.class.getClassLoader());
    this.mModifiedDate = in.readString();
    this.mPhone = new ArrayList<Phone>();
    in.readList(this.mPhone, Phone.class.getClassLoader());
    this.mPhoto = in.readString();
    this.mSalutation = in.readString();
    this.mSubscriptionId = in.readString();
    this.mType = in.readString();
    this.isChecked = in.readByte() != 0;
}

  @Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.mContactId);
    dest.writeString(this.mAssignedTo);
    dest.writeString(this.mCreatedDate);
    dest.writeString(this.mFirstName);
    dest.writeString(this.mImg);
    dest.writeString(this.mLastName);
    dest.writeList(this.mLinks);
    dest.writeString(this.mModifiedDate);
    dest.writeList(this.mPhone);
    dest.writeString(this.mPhoto);
    dest.writeString(this.mSalutation);
    dest.writeString(this.mSubscriptionId);
    dest.writeString(this.mType);
    dest.writeByte(this.isChecked ? (byte) 1 : (byte) 0);
}

For easy understanding and generation of Parcelable i suggest you to add Android Parcelable code generator plugin in IDE.
GOTO Setting>Plugins>Browse Repositories> Type Parcelable

ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1) i already use plugin for that ...you show all class and variable and methods in that code...its all created by that plugins 2) Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@536bd848: Unmarshalling unknown type code 3538995 at offset 516 at .designingapp.pojo.Datum.(Datum.java:58) redirect this line :in.readList(this.mLinks, Link.class.getClassLoader()); – BiRjU Mar 15 '18 at 09:23
  • Double check order writing and read from `Parcel`.Whats on Line `Datum.java:58` can you indicate ? – ADM Mar 15 '18 at 09:27
  • redirect this line :in.readList(this.mLinks, Link.class.getClassLoader()); – BiRjU Mar 15 '18 at 09:44
  • check order writing and read.....means i have to maintain which structer to declare data...thats structure i init counstructer ??? – BiRjU Mar 15 '18 at 09:47
  • NO constructor only write and read `Parcel`. I think order is OK. have a look at [This discussion](https://stackoverflow.com/questions/19672772/parcel-unmarshalling-unknown-type-code) – ADM Mar 15 '18 at 09:52
  • i delete my old class i re generate my class from http://www.parcelabler.com/ that gives me if (in.readByte() == 0x01) { mLinks = new ArrayList(); in.readList(mLinks, Link.class.getClassLoader()); } else { mLinks = null; } mModifiedDate = in.readString(); if (in.readByte() == 0x01) { mPhone = new ArrayList(); in.readList(mPhone, Phone.class.getClassLoader()); } else { mPhone = null; } – BiRjU Mar 15 '18 at 12:52
  • yes...after that process...it is solved... but i understand don't ever used plugin for parcelable...i know online convert process is maybe long but...its never disappointed.. – BiRjU Mar 15 '18 at 13:18
  • Well answer your question then . Accepted answer should be in post . – ADM Mar 15 '18 at 13:19
  • and one more thing...i also check after switch my variable position from writeToParcel() method....and its not work....its only work when and when the order of variable declaring is all same – BiRjU Mar 15 '18 at 13:22
  • Yeah read /write ordering should be same cause In `Parcelable` we are dealing with `byte[]` not with `Map`. – ADM Mar 15 '18 at 13:23
0

first sorry because my English is bad

you are missing write and read link and phone list just write code exactly ADM code it's work fine

add in your constructor

mLinks = new ArrayList<Link>();
in.readList(this.mLinks, Link.class.getClassLoader());
mPhone = new ArrayList<Phone>();
in.readList(this.mPhone, Phone.class.getClassLoader());

and writeToParcel()

dest.writeList(mLinks);
dest.writeList(mPhone);