Search code examples
parceler

How to pass the Arraylist of custom Object type from activity to Service using the Parceler library?


I have done this in activity.Here responses variable is the ArrayList of type custom object LeaseDetailResponse.

 Intent intent = new Intent(TestActivity.this, AlarmService.class);
    intent.putParcelableArrayListExtra(AlarmService.LEASE_DETAIL_RESPONSE_LIST, (ArrayList<? extends Parcelable>) Parcels.wrap(responses));
    startService(intent);

At AlarmService

Parcels.unwrap((Parcelable) intent.getParcelableArrayListExtra(LEASE_DETAIL_RESPONSE_LIST));

Shows the error

    java.lang.ClassCastException: org.parceler.NonParcelRepository$ListParcelable cannot be cast to java.util.ArrayList

  Caused by: java.lang.ClassCastException: org.parceler.NonParcelRepository$ListParcelable cannot be cast to java.util.ArrayList

Solution

  • The issue is with your cast to an ArrayList... Parceler only deals with Parcelable. You need to use putExtra() instead with no cast:

    Intent intent = new Intent(TestActivity.this, AlarmService.class);
    intent.putExtra(AlarmService.LEASE_DETAIL_RESPONSE_LIST, Parcels.wrap(responses));
    startService(intent);
    

    And to de-serialize in your AlarmService:

    Parcels.unwrap(intent.getParcelableExtra(LEASE_DETAIL_RESPONSE_LIST));