2014-01-09 42 views
1

我正在使用自定义parcelable类。我需要将这个类作为数组传递给另一个活动和新的片段。我GOOGLE了,但我发现arrayList,但不是数组。请介意我需要传递数组而不是数组列表。如何实现这一目标?请再说一遍,性能是一个大问题。所以任何性能消耗较少的解决方案都非常受欢迎。如何将Parcelable数组传递给fragment和Activity

任何帮助将不胜感激。

+0

检查我的编辑评论 – user1621629

回答

1

这里是写答案

// 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"); 
+2

分别是MyParcelable类 –

+0

它是实现Parcelable(接口)的任何类... – deHaar

4

你可以像下面的snipts一样传递你的arrayList。

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

并获得这些型号的数据parcable像如下:

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

YES,序列化是慢一点比Parcelable。

而且你可以实现使用parceler

当然,ParcelWrapper可以加入到Android捆绑使用这个从活动转移到活动:

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

并取消引用在OnCreate()方法:

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

你可以得到更多关于Parcelable与Serializable相关性能的细节here

+0

不是一个很好的解决方案。消耗很多性能。 – Mesut

+0

是的,因为序列化与Parcable相比太慢。 – Piyush

+0

请看我的肛门。 – Mesut

0

使用像下面的类你parcealble阵列

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]; 
    } 
    }; 
} 

现在做ParcelableLaptop的ArrayList中,并放在同捆参数

intent.putParcelableArrayListExtra("laptop", parcelableLaptopArray); 

哪里parcelableLaptop是你的模型的ArrayList中。并获得名单:

Intent intent = getIntent(); 

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

ParcelableLaptop parcelableLaptop =(ParcelableLaptop)intent .getParcelableExtra(“laptop”); 它接缝一个物体。但我需要对象数组。 – Mesut

+0

我已编辑代码请检查此...如果您有任何问题,请让我知道 –

+0

您的代码看起来awsum。请检查一下。另外我需要数组而不是arrayList。代码必须是这样的:ParcelableLaptop [] – Mesut

相关问题