0

I want to send model object to another activity,which contains list refer to another model

public class row_my_bookings implements Parcelable {

String BookingID;
ArrayList<MainService> list_of_mainservice;

 public row_my_bookings(Parcel in) {
     String[] data = new String[3];
    in.readStringArray(data);
       this.list_of_mainservice = in.readArrayList(null);
}

@Override
public void writeToParcel(Parcel dest, int flags) {

 dest.writeList(this.list_of_mainservice);   // Error shows in this line
}

 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public row_my_bookings createFromParcel(Parcel in) {
        return new row_my_bookings(in);
    }

    public row_my_bookings[] newArray(int size) {
        return new row_my_bookings[size];
    }
};
}  

row_main_services model class

public class row_main_services implements Parcelable{

String ServiceId, 
 public MainService(Parcel in) {
    String[] data = new String[3];
    in.readStringArray(data);
    this.id = in.readString();
    this.name=in.readString();
    if (in.readByte() == 0x01) {
        list_child_services = new ArrayList<row_child_services>();
        in.readList(list_child_services, row_child_services.class.getClassLoader());
    } else {
        list_child_services = null;
    }
   }

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(this.id);
    dest.writeString(this.name);
    if (list_child_services == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(list_child_services);
    }
}

public static final Parcelable.Creator<MainService> CREATOR = new Parcelable.Creator<MainService>() {
    public MainService createFromParcel(Parcel in) {
        return new MainService(in);
    }

    public MainService[] newArray(int size) {
        return new MainService[size];
    }
};

Third module

public class row_child_services implements Parcelable{

String subId,subName;
public row_child_services(Parcel in) {
    String[] data = new String[3];

    in.readStringArray(data);
    this.subId = in.readString();
    this.subName = in.readString();
 }
 @Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.subId);
    dest.writeString(this.subName);      
}
 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public row_child_services createFromParcel(Parcel in) {
        return new row_child_services(in);
    }

    public row_child_services[] newArray(int size) {
        return new row_child_services[size];
    }
};

I am sending object like following way

list_Bookings=new ArrayList<row_my_bookings>(rowItems);
intentBookingWindow.putParcelableArrayListExtra("BookingObject", list_Bookings);
Shruti
  • 391
  • 1
  • 5
  • 21

1 Answers1

0

the row_my_bookings class implements Parcelable but i guess MainService class does not implemented Parcelable and thats your problem. you must make this class Parcelable too.

I recomand you to change your model like this : You have imported "MainService" class which is not the class you send later. I guess you must change this class to row_main_services and then reconsider your Parcelable implementations ;)

public class row_my_bookings implements Parcelable {

String BookingID;
ArrayList<row_main_services> list_of_mainservice; // i guess you must change this line 

protected row_my_bookings(Parcel in) {
    BookingID = in.readString();
    if (in.readByte() == 0x01) {
        list_of_mainservice = new ArrayList<MainService>();
        in.readList(list_of_mainservice, MainService.class.getClassLoader());
    } else {
        list_of_mainservice = null;
    }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(BookingID);
    if (list_of_mainservice == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(list_of_mainservice);
    }
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<row_my_bookings> CREATOR = new Parcelable.Creator<row_my_bookings>() {
    @Override
    public row_my_bookings createFromParcel(Parcel in) {
        return new row_my_bookings(in);
    }

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

clss row_main_services:

public class row_main_services implements Parcelable {

String ServiceId, 
ArrayList<row_child_services> list_row_child_services;

protected row_main_services(Parcel in) {
    if (in.readByte() == 0x01) {
        list_row_child_services = new ArrayList<row_child_services>();
        in.readList(list_row_child_services, row_child_services.class.getClassLoader());
    } else {
        list_row_child_services = null;
    }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (list_row_child_services == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(list_row_child_services);
    }
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<row_main_services> CREATOR = new Parcelable.Creator<row_main_services>() {
    @Override
    public row_main_services createFromParcel(Parcel in) {
        return new row_main_services(in);
    }

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

And here is your third class:

public class row_child_services implements Parcelable {

String subId;
String subName;

    protected row_child_services(Parcel in) {
        subId = in.readString();
        subName = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(subId);
        dest.writeString(subName);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<row_child_services> CREATOR = new Parcelable.Creator<row_child_services>() {
        @Override
        public row_child_services createFromParcel(Parcel in) {
            return new row_child_services(in);
        }

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

And about the last error you've got, I suggest you to take a look at this answer.

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78