6

This question is different from all the other questions because here the list of parcelable objects is created outside on the fly just sending it before via Intent.

Problem: Product is parcelable but the custom List<Product> is not. How to make the List Parcelable??

Product

public class Product implements Parcelable {

    public String id;
    public String name;

    public Product(String id, String name) {
        this.id = id;
        this.name = name;
    }

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

    protected Product(Parcel in) {
        id = in.readString();
        name = in.readString();
    }

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

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(id);
        parcel.writeString(name);
    }
}

Activity class

Intent intent = new Intent(this, ShowProducts.class);
List<Product> products = getSpecificProducts();

intent.putExtra("PRODUCTS", products); // error

startActivity(intent);

Edit

The question is two fold, it's not a duplicate, the other part is List<Enum>

public enum Department implements Parcelable {

    SALES("Sales", 0),
    HR("HR", 1),

    public String name;
    public int ordinal;

    Department(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }

    public static final Creator<Department> CREATOR = new Creator<Department>() {
        @Override
        public Department createFromParcel(Parcel in) {
            return Department.values()[in.readInt()];
        }

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

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(ordinal);
    }

}

Based on the suggested answers, I've tried.

List<Department> departments = getSpecificDepartments();

intent.putParcelableArrayListExtra("Departments", departments);

error: incompatible types: List<Department> cannot be converted to ArrayList<? extends Parcelable>

GensaGames
  • 5,538
  • 4
  • 24
  • 53
AppDeveloper
  • 1,816
  • 7
  • 24
  • 49
  • With Pojo, You can try to encode it into JSON and parse it later via Gson from Google. – Harry T. Oct 25 '19 at 16:34
  • @AdmiralKunkka Are you sure about that? – GensaGames Oct 25 '19 at 16:57
  • Possible duplicate of [Pass ArrayList implements Parcelable> to Activity](https://stackoverflow.com/questions/15133121/pass-arraylist-implements-parcelable-to-activity) – GensaGames Oct 25 '19 at 16:58
  • @GensaGames your answer is quite correct. I just give another option to solve his problem. And yes, any plain java object or list contain those objects can be serializable to JSON by Gson. – Harry T. Oct 25 '19 at 16:59
  • @AdmiralKunkka Are you sure? Another options to double serialize to JSON, to Parcelable and then double deserialize back? – GensaGames Oct 25 '19 at 17:03
  • @GensaGames I mean encode to JSON string, send Extra with `.putString("data", str)`. And then later in next activity, parsing it back to Object (or List) to use. – Harry T. Oct 25 '19 at 17:06

3 Answers3

5

You don't need to make List parcelable. To put it, just use existing method.

final Intent i = new Intent(this,SecondActivity.class);
final ArrayList<Product> list = new ArrayList< Product >();
i.putParcelableArrayListExtra(PRODUCT_KEY, list);
startActivity(i);

To retrieve almost in the same way.

final ArrayList<Product> list = this.getIntent()
            .getParcelableArrayListExtra(PRODUCT_KEY);

UPDATE

To answer you newer question. You cannot pass generic List inside. Because by List you could have different collection. To know system, which type to use you need to have explicit type.

// This will work. 
final ArrayList<Department> departments = getSpecificDepartments();
intent.putParcelableArrayListExtra(DEPARTMENTS_KEY, departments);
GensaGames
  • 5,538
  • 4
  • 24
  • 53
2

Use putParcelableArrayListExtra method like this:

ArrayList<Product> products = getSpecificProducts();
intent.putParcelableArrayListExtra("PRODUCTS", products);
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Sina
  • 2,683
  • 1
  • 13
  • 25
  • 1
    We Practically posted at the same time. I saw the question and answered it. I just didn't refresh the page before posting the answer. What's with the down vote dude? This is a community to help others. Judgement for correct answer is with the person who asked it, which he/her probably will accept your answer since it is posted earlier. You are misleading others with down votes. – Sina Oct 25 '19 at 17:30
  • sorry i stepped out a bit and came back to desk, I actually have this as a two issue, please see my edit, your answer is right, if you can add the other extra bit, I'd really appreciate. – AppDeveloper Oct 25 '19 at 18:01
  • 1
    You should use an ArrayList not a List. – Sina Oct 25 '19 at 18:23
0

Try this

var intent = Intent(this, ActivityB::class.java)
val list = arrayListOf<Person>()
intent.putParcelableArrayListExtra("LIST", list)