Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将包裹数组传递给碎片和活动_Java_Android - Fatal编程技术网

Java 如何将包裹数组传递给碎片和活动

Java 如何将包裹数组传递给碎片和活动,java,android,Java,Android,我正在使用定制包裹类。我需要将这个类作为数组传递给另一个活动和新的片段。我在谷歌上搜索过,但找到了arrayList,但没有找到数组。请注意,我需要传递数组而不是数组列表。如何做到这一点?请注意,性能是个大问题。因此,任何性能消耗较少的解决方案都是非常受欢迎的 任何帮助都将不胜感激。您可以通过下面的列表 TestFragment testFragment = new TestFragment(); Bundle args = new Bundle(); args.putSerializab

我正在使用定制包裹类。我需要将这个类作为数组传递给另一个活动和新的片段。我在谷歌上搜索过,但找到了arrayList,但没有找到数组。请注意,我需要传递数组而不是数组列表。如何做到这一点?请注意,性能是个大问题。因此,任何性能消耗较少的解决方案都是非常受欢迎的


任何帮助都将不胜感激。

您可以通过下面的列表

 TestFragment testFragment = new TestFragment();
 Bundle args = new Bundle();
 args.putSerializable("parsedData", (Serializable)mTestModelList);
 testFragment.setArguments(args);
并在模型中获取这些可分配数据,如下所示:

mTestModelList = (List<TestModel>)getArguments().get("parsedData");
并在onCreate()方法中取消引用:


您可以获得与Parcelable和Serializable相关的更多性能细节。

您可以通过arrayList,如下所示

 TestFragment testFragment = new TestFragment();
 Bundle args = new Bundle();
 args.putSerializable("parsedData", (Serializable)mTestModelList);
 testFragment.setArguments(args);
并在模型中获取这些可分配数据,如下所示:

mTestModelList = (List<TestModel>)getArguments().get("parsedData");
并在onCreate()方法中取消引用:


您可以获得有关Parcelable与Serializable的性能的更多详细信息。

使用Parcelable数组的以下类

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是模型的阵列列表。要获取列表,请执行以下操作:

 Intent intent = getIntent();

 ArrayList<ParcelableLaptop> parcelableLaptop = (ParcelableLaptop) intent
         .getParcelableArrayListExtra("laptop");
Intent-Intent=getIntent();
ArrayList parcelableLaptop=(parcelableLaptop)意图
.getParcelableArrayListExtra(“笔记本电脑”);

使用类似以下类别的可折叠阵列

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是模型的阵列列表。要获取列表,请执行以下操作:

 Intent intent = getIntent();

 ArrayList<ParcelableLaptop> parcelableLaptop = (ParcelableLaptop) intent
         .getParcelableArrayListExtra("laptop");
Intent-Intent=getIntent();
ArrayList parcelableLaptop=(parcelableLaptop)意图
.getParcelableArrayListExtra(“笔记本电脑”);

以下是书面答案

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

这是书面答案

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


对因为序列化与可打包相比太慢了。请看我的答案。是的。。因为序列化与可打包相比太慢。请查看我的anser.ParcelableLaptop ParcelableLaptop=(ParcelableLaptop)intent.getParcelableExtra(“笔记本”);它缝合一个物体。但我需要的对象数组。我已经编辑了代码请检查这个。。。如果您有任何问题,请告诉我您的代码看起来有问题。请检查一下。我还需要数组而不是arrayList。代码必须是这样的:ParcelableLaptop[]请查看我的anser.ParcelableLaptop ParcelableLaptop=(ParcelableLaptop)intent.getParcelableExtrap(“笔记本”);它缝合一个物体。但我需要的对象数组。我已经编辑了代码请检查这个。。。如果您有任何问题,请告诉我您的代码看起来有问题。请检查一下。我还需要数组而不是arrayList。代码必须是这样的:ParcelableLaptop[]请查看我的答案。检查我编辑的注释检查我编辑的注释是MyParcelable Classis是实现Parcelable(接口)的任何类…是MyParcelable Classis是实现Parcelable(接口)的任何类…