简体   繁体   中英

How to pass Parcelable array to fragment and Activity

I'm working with custom parcelable class. I need to pass this class as array to another activity and new fragments. I googled, but I found arrayList but not arrays. Please mind that I need to pass arrays not Array lists. How to achieve that? Please mind again, performance is a big issue. So any solutions with less performance consumption is very welcome.

Any helps would be very appreciated.

You can pass your arrayList as below snipts.

 TestFragment testFragment = new TestFragment();
 Bundle args = new Bundle();
 args.putSerializable("parsedData", (Serializable)mTestModelList);
 testFragment.setArguments(args);

And Get these parcable data in Model as like below:

mTestModelList = (List<TestModel>)getArguments().get("parsedData");

YES, Serialisation is slow bit than Parcelable.

And you can implement that using parceler

Of course, the ParcelWrapper can be added to an Android Bundle to transfer from Activity to Activity using this:

Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));

And dereferenced in the onCreate() method:

Example example = Parcels.unwrap(getIntent().getParcelableExtra("example"));

You can get more detail of performance related to Parcelable vs Serializable here .

Here is the write answer

// write parcelable array
final Bundle arguments = new Bundle();
MyParcelable[] myArray = new MyParcelable[10];
arguments.putParcelableArray("key"myArray);

// read parcelable array
MyParcelable[] myArray = (MyParcelable[])getArguments().getParcelableArray("key");

use like following class of your parcealble array

public class ParcelableLaptop implements Parcelable {
private Laptop laptop;

public Laptop getLaptop() {
    return laptop;
}

public ParcelableLaptop(Laptop laptop) {
    super();
    this.laptop = laptop;
}

private ParcelableLaptop(Parcel in) {
    laptop = new Laptop();
    laptop.setId(in.readInt());
    laptop.setBrand(in.readString());
    laptop.setPrice(in.readDouble());
    laptop.setImageBitmap((Bitmap) in.readParcelable(Bitmap.class
            .getClassLoader()));
}

/*
 * you can use hashCode() here.
 */
@Override
public int describeContents() {
    return 0;
}

/*
 * Actual object Serialization/flattening happens here. You need to
 * individually Parcel each property of your object.
 */
@Override
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeInt(laptop.getId());
    parcel.writeString(laptop.getBrand());
    parcel.writeDouble(laptop.getPrice());
    parcel.writeParcelable(laptop.getImageBitmap(),
            PARCELABLE_WRITE_RETURN_VALUE);
}

/*
 * Parcelable interface must also have a static field called CREATOR,
 * which is an object implementing the Parcelable.Creator interface.
 * Used to un-marshal or de-serialize object from Parcel.
 */
public static final Parcelable.Creator<ParcelableLaptop> CREATOR =
        new Parcelable.Creator<ParcelableLaptop>() {
    public ParcelableLaptop createFromParcel(Parcel in) {
        return new ParcelableLaptop(in);
    }

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

now make Arraylist of ParcelableLaptop and put in the bUndle with argument

intent.putParcelableArrayListExtra("laptop", parcelableLaptopArray);

Where parcelableLaptop is your Arraylist of the Model. And to get the List:

 Intent intent = getIntent();

 ArrayList<ParcelableLaptop> parcelableLaptop = (ParcelableLaptop) intent
         .getParcelableArrayListExtra("laptop");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM