Read & Writing Arrays of Parcelable Objects

Read & writing arrays of Parcelable objects

You need to write the array using the Parcel.writeTypedArray() method and read it back with the Parcel.createTypedArray() method, like so:

MyClass[] mObjList;

public void writeToParcel(Parcel out) {
out.writeTypedArray(mObjList, 0);
}

private void readFromParcel(Parcel in) {
mObjList = in.createTypedArray(MyClass.CREATOR);
}

The reason why you shouldn't use the readParcelableArray()/writeParcelableArray() methods is that readParcelableArray() really creates a Parcelable[] as a result. This means you cannot cast the result of the method to MyClass[]. Instead you have to create a MyClass array of the same length as the result and copy every element from the result array to the MyClass array.

Parcelable[] parcelableArray =
parcel.readParcelableArray(MyClass.class.getClassLoader());
MyClass[] resultArray = null;
if (parcelableArray != null) {
resultArray = Arrays.copyOf(parcelableArray, parcelableArray.length, MyClass[].class);
}

Android: Can I pass an array of Parcelable objects? Do I need to create a wrapper object?

Do something like this to write an array list into Parcelable:

public class Task implements Parcelable {
List<Task> taskList;

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(taskList);
...
}

@SuppressWarnings("unchecked")
private Task(Parcel in) {
taskList = (List<Task>) in.readValue(List.class.getClassLoader());
...
}
}

It 100% works for me.

Write and Read an object array to parcelable

I managed to find the correct solution (for me) so if anyone else is coming up against this, follow this link:
http://www.javacreed.com/gson-deserialiser-example/

It shows how to parse nested array and deserialize.

Writing/reading an ArrayList of Parcelable objects

The links suggested by both George and buczek can be really helpful in your case.
Perhaps more directly, you need to do something like this:

//Supposing you declared mClassBList as:
List<ClassB> mClassBList = new ArrayList<ClassB>();
in.readTypedList(mClassBList, ClassB.CREATOR);

I still recommend you check out the answer, especially (as suggested by Buczek) How to pass a parcelable object that contains a list of objects?

How to parcelable an object with an array of another object

Use an ArrayList instead of an Array and make Answer class Parceable then update your code to the following:

  public class Quiz implements Parcelable {

int event_id;
String name;
String business_unit;
int functional_area_business; // 0 unchecked, 1 checked
int functional_area_support; // 0 unchecked, 1 checked
List<Answer > list_answers;

//GETTERS AND SETTERS
...
//

public static final Parcelable.Creator<Quiz> CREATOR = new Parcelable.Creator<Quiz>() {
public Quiz createFromParcel(Parcel in) {
return new Quiz(in);
}

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(event_id);
dest.writeString(name);
dest.writeString(business_unit);
dest.writeInt(functional_area_business);
dest.writeInt(functional_area_support);
dest.writeTypedList(list_answers);
}

private Quiz(Parcel in) {
event_id = in.readInt();
name = in.readString();
business_unit = in.readString();
functional_area_business = in.readInt();
functional_area_support = in.readInt();
list_answers = new ArrayList<Answer>();
in.readTypedList(list_answers , Answer.CREATOR);
}
}

And this is the Parceable Answer:

    public class Answer implements Parcelable  {

int answer_id;
String value;

public static final Parcelable.Creator<Answer> CREATOR = new Parcelable.Creator<Answer>() {
public Answer createFromParcel(Parcel in) {
return new Answer(in);
}

public Answer[] newArray(int size) {
return new Answer[size];
}
};
@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(answer_id);
dest.writeString(value);

}
private Answer(Parcel in) {
answer_id = in.readInt();
value = in.readString();
}

}

Writing/Reading arrays or Stringarrays in Parcelable

The Parcel class has a writeStringArray() method. Couldn't your class iterate over the String[][] and call writeStringArray for each?

Assuming your class has a member:

String[][] data;

in writeToParcel()

parcel.writeInt(data.length);
for( int i=0; i<data.lengh; i++ ){
parcel.writeStringArray(data[i]);
}

in createFromParcel()

int size = parcel.readInt();
data = new String[size][];
for( int i=0; i<size; i++ ){
data[i] = parcel.readStringArray();
}

Read and write array of own type in parcel in android

Use writeTypedArray() and createTypedArray().

Also your options are an array of Strings in your model while in the json is clearly a list of key-value pairs. You should probably use 2 arrays (one for keys and one for values) or a map-like Bundle instead.

How to include an array of Parcelable objects with a savedInstanceStateBundle

Reading Parcelable arrays directly from the bundle will not work. Example:

Pair[] pairs = savedInstanceState.getParcelableArray(QUESTIONS_CHEATED);

So this is how I read Parcelable arrays:

Parcelable[] parcelables = savedInstanceState.getParcelableArray(QUESTIONS_CHEATED);
Pair[] pairs = new Pair[parcelables.length];
for(int i = 0; i < parcelables.length; i++) {
pairs[i] = (Pair) parcelable[i];
}

If someone knows a better way, I would like to know please.



Related Topics



Leave a reply



Submit