我正在尝试使以下类可以使用。通常,我通过使用parcelabler网站来做到这一点。但该网站似乎存在ArrayList>的问题。部分。有人知道如何正确地做到这一点吗? 提前谢谢!
package com.example.stoos.data;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class DataModelDiagrammDaten implements Parcelable {
ArrayList<String> Antworten = new ArrayList<String>();
ArrayList<ArrayList<Integer>> AntwortIDs = new ArrayList<ArrayList<Integer>>();
public ArrayList<String> getAntworten() {
return Antworten;
}
public ArrayList<ArrayList<Integer>> getAntwortIDs() {
return AntwortIDs;
}
public void setAntworten(ArrayList<String> antworten) {
Antworten = antworten;
}
public void setAntwortIDs(ArrayList<ArrayList<Integer>> antwortIDs) {
AntwortIDs = antwortIDs;
}
public DataModelDiagrammDaten(ArrayList<String> antworten,
ArrayList<ArrayList<Integer>> antwortIDs) {
super();
Antworten = antworten;
AntwortIDs = antwortIDs;
}
protected DataModelDiagrammDaten(Parcel in) {
if (in.readByte() == 0x01) {
Antworten = new ArrayList<String>();
in.readList(Antworten, String.class.getClassLoader());
} else {
Antworten = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (Antworten == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(Antworten);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<DataModelDiagrammDaten> CREATOR = new Parcelable.Creator<DataModelDiagrammDaten>() {
@Override
public DataModelDiagrammDaten createFromParcel(Parcel in) {
return new DataModelDiagrammDaten(in);
}
@Override
public DataModelDiagrammDaten[] newArray(int size) {
return new DataModelDiagrammDaten[size];
}
};
}
答案 0 :(得分:1)
我认为这有点晚了,但希望它对其他人有用。 我试着像这样写一个二维数组到Parcel:
public class Avatar implements Parcelable {
ArrayList<Integer> arrayList;
ArrayList<ArrayList<Integer>> arrayLists;
public Avatar() {
arrayLists = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ArrayList<Integer> temp = new ArrayList<>();
for (int j = 0; j < 10; j++) {
temp.add(j);
}
arrayLists.add(temp);
}
arrayList = new ArrayList<>();
for (int i = 100; i < 104; i++) {
arrayList.add(i);
}
}
protected Avatar(Parcel in) {
arrayList = new ArrayList<>();
in.readList(arrayList, Integer.class.getClassLoader());
arrayLists = new ArrayList<>();
in.readList(arrayLists, ArrayList.class.getClassLoader());
}
public static final Creator<Avatar> CREATOR = new Creator<Avatar>() {
@Override
public Avatar createFromParcel(Parcel in) {
return new Avatar(in);
}
@Override
public Avatar[] newArray(int size) {
return new Avatar[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(arrayList);
dest.writeList(arrayLists);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayList.size(); i++) {
stringBuilder.append(arrayList.get(i) + " ");
}
for (int i = 0; i < arrayLists.size(); i++) {
for (int j = 0; j < arrayLists.get(i).size(); j++) {
stringBuilder.append(arrayLists.get(i).get(j) + " ");
}
}
return stringBuilder.toString();
}
}
将课程发送到另一个活动:
avatar = new Avatar();
Intent intent = new Intent();
intent.setClass(ParcelActivity.this, Parcel2Activity.class);
intent.putExtra("avatar", avatar);
startActivity(intent);
接收另一项活动:
textView.setText(getIntent().getParcelableExtra("avatar") + "");
它正常工作: test
应该注意的是,Parcel中字段对象的写入和读取必须是相同的顺序。
答案 1 :(得分:0)
修改强>:
尝试将第二个ArrayList放在另一个类中:
public class IdsObject implements Parcelable {
ArrayList<Integer> ids;
public IdsObject(ArrayList<Integer> ids) {
this.ids = ids;
}
protected IdsObject(Parcel in) {
if (in.readByte() == 0x01) {
ids = new ArrayList<Integer>();
in.readList(ids, Integer.class.getClassLoader());
} else {
ids = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (ids == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(ids);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<IdsObject> CREATOR = new Parcelable.Creator<IdsObject>() {
@Override
public IdsObject createFromParcel(Parcel in) {
return new IdsObject(in);
}
@Override
public IdsObject[] newArray(int size) {
return new IdsObject[size];
}
};
}
然后,在你的课堂上使用这个课程:
public class DataModelDiagrammDaten implements Parcelable, Parcelable {
ArrayList<String> Antworten = new ArrayList<String>();
ArrayList<IdsObject> AntwortIDs = new ArrayList<IdsObject>();
protected DataModelDiagrammDaten(Parcel in) {
if (in.readByte() == 0x01) {
Antworten = new ArrayList<String>();
in.readList(Antworten, String.class.getClassLoader());
} else {
Antworten = null;
}
if (in.readByte() == 0x01) {
AntwortIDs = new ArrayList<IdsObject>();
in.readList(AntwortIDs, IdsObject.class.getClassLoader());
} else {
AntwortIDs = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (Antworten == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(Antworten);
}
if (AntwortIDs == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(AntwortIDs);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<DataModelDiagrammDaten> CREATOR = new Parcelable.Creator<DataModelDiagrammDaten>() {
@Override
public DataModelDiagrammDaten createFromParcel(Parcel in) {
return new DataModelDiagrammDaten(in);
}
@Override
public DataModelDiagrammDaten[] newArray(int size) {
return new DataModelDiagrammDaten[size];
}
};
}
答案 2 :(得分:0)
好的,我解决了。如果其他人遇到同样的问题,这是解决方案: 您可以先使用可与网站配对的普通ArrayList(而不是ArrayList&gt;)来创建您的类。然后使用以下代码片段将其写入构造函数,并在该位置使用Parcel参数,如果是ArrayList的话。现在将ArrayList更改回ArrayList&gt;它应该工作。
if (in.readByte() == 0x01) {
IDs = new ArrayList<ArrayList<Integer>>();
Type collectionType = new TypeToken<ArrayList<Integer>>(){}.getType();
in.readList(IDs, collectionType.getClass().getClassLoader());
} else {
IDs = null;
}