利用Parcelable在两个活动之间发送数组

时间:2019-03-08 00:24:27

标签: java android parcelable

我正在尝试在两个活动之间发送一个填充的数组,该数组的创建的类名为'Exercise',并且我已经阅读了使用Parcelable类的最佳方法。

>

活动2每次启动都会崩溃。我已经在网上关注了这些教程,这就是他们如何处理数据发送的方法。

运动课:

import android.os.Parcel;
import android.os.Parcelable;

public class Exercise implements Parcelable {

    public String Name = "";
    public int Time = 0;
    public boolean SwitchDirection = false;

    Exercise(String n, int l, boolean s){
        Name = n;
        Time = l;
        SwitchDirection = s;
    }

    protected Exercise(Parcel in) {
        Name = in.readString();
        Time = in.readInt();
        SwitchDirection = in.readByte() != 0;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(Name);
        dest.writeInt(Time);
        dest.writeByte((byte) (SwitchDirection ? 1 : 0));
    }

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

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

活动1:

     Exercise[] Exercises = new Exercise[]{
             new Exercise("Scisssors", 60, false),
             new Exercise("Cross Knee Planks", 60, false),
             new Exercise("Twisting Pistons", 30, false),
             new Exercise("Accordion Crunches", 60, true),
             new Exercise("Canoe Crunch", 30, false),
             new Exercise("Upper Circle Crunches", 30, true)
    };


    Intent i = new Intent(this, Activity2.class);
    i.putExtra("Exercises", Exercises);
    startActivity(i);

活动2:

    Exercise[] Exercises;
    Exercises = (Exercise[]) getIntent().getParcelableArrayExtra("Exercises");

1 个答案:

答案 0 :(得分:0)

我认为您不应该发送和获取Parcelable数组。您应该为Parcelable提供一个数组,并在活动2中获取它。 我有一个示例在2个活动之间发送HashMap。

public class BaseData implements Parcelable {

protected HashMap<String, Object> mData;

public BaseData(HashMap<String, Object> data) {
    mData = data;
}

protected BaseData(Parcel in) {
    try {
        mData = in.readHashMap(HashMap.class.getClassLoader());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    try {

        if (null == mData) {
            return;
        }

        dest.writeValue(mData);
    } catch (Exception e) {

    }
}

public HashMap<String, Object> getData() {
    return mData;
}

}

在活动1中,您将像这样发送数据:

  HashMap<String, Object> hm = new HashMap<>();
    hm.put("data_1", array_data_1);
    hm.put("data_2",array_data_2);
    BaseData baseData = new BaseData(hm);
    BaseData baseData = new BaseData(data);
    Bundle bundle = new Bundle();
    bundle.putParcelable("base_data", baseData);
    Intent intent = new Intent(context,Activity2.class)
    intent.putExtra("data",bundle);

在Activy 2中,您将收到一个包,然后从包中获取basedata,并从basedata中获取HashMap。

   Bundle bundle = getIntent().getBundleExtra("data");
    BaseData baseData = bundle.getParcelable("base_data");
    HashMap<String,Object> hm = baseData.getData();

您可以用Array代替HashMap。如果您无法实现,请告诉我,我将向您发送完整的示例。