how to properly implement Parcelable with an ArrayListParcelable?
You almost got it !
You just need to do :
public void writeToParcel(Parcel out, int flags) {
out.writeString(_mac);
out.writeString(_pan);
out.writeInt(_band);
out.writeSerializable(_lqis);
out.writeTypedList(_devices);
}
private ZigBeeNetwork(Parcel in) {
_mac = in.readString();
_pan = in.readString();
_band = in.readInt();
_lqis = (ArrayList<Integer>) in.readSerializable();
in.readTypedList(_devices, ZigBeeDev.CREATOR);
}
That's all!
For your list of Integer, you can also do :
out.writeList(_lqis);
_lqis = new ArrayList<>();
in.readList(_lqis Integer.class.getClassLoader());
It should work.
How to implement Parcelable on my class which contains an ArrayListString?
I'm using Java instead of Kotlin but I think this problem can handled by a same solution.
I often use:
- parcel.createStringArrayList() instead of parcel.readArrayList(null)
- parcel.writeStringList(transactionImages) instead of parcel.writeList(transactionImages)
How to write to arraylist of two or more custom objects in Parcelable?
You can add customArray using following way :
In MovieObject
class:
@Expose
private ArrayList< ReviewObject > ReviewObjs = new ArrayList< ReviewObject >();
private ArrayList< TrailerVideoObject > TrailerVideoObjs = new ArrayList< TrailerVideoObject >();
writeToParcel() :
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(ReviewObjs);
dest.writeTypedList(TrailerVideoObjs);
}
and readFromParcel() :
private void readFromParcel(Parcel in) {
ReviewObjs = new ArrayList<ReviewObject>();
in.readTypedList(ReviewObjs, ReviewObject.CREATOR);
TrailerVideoObjs = new ArrayList<TrailerVideoObject>();
in.readTypedList(TrailerVideoObjs, TrailerVideoObject.CREATOR);
}
And add following lines in CustomClasses :
for ReviewObject
:
public class ReviewObject implements Parcelable {
public static final Creator<ReviewObject> CREATOR = new Creator<ReviewObject>() {
public ReviewObject createFromParcel(Parcel in) {
return new ReviewObject(in);
}
public ReviewObject[] newArray(int size) {
return new ReviewObject[size];
}
};
// add other objects...
}
for TrailerVideoObject
:
public class TrailerVideoObject implements Parcelable {
public static final Creator<TrailerVideoObject> CREATOR = new Creator<TrailerVideoObject() {
public TrailerVideoObject createFromParcel(Parcel in) {
return new TrailerVideoObject(in);
}
public TrailerVideoObject[] newArray(int size) {
return new TrailerVideoObject[size];
}
};
// add other objects...
}
Android Class Parcelable with ArrayList
If you need to pass an ArrayList
between activities, then I'd go with implementing Parcelable
also, as there is no other way around I guess.
However I don't think you will need that much of getters and setters. Here is your Question
class which implements Parcelable
:
public class Question implements Parcelable {
public String id;
public String text;
public String image;
public ArrayList<Choice> choices;
/**
* Constructs a Question from values
*/
public Question (String id, String text, String image, ArrayList<Choice> choices) {
this.id = id;
this.text = text;
this.image = image;
this.choices = choices;
}
/**
* Constructs a Question from a Parcel
* @param parcel Source Parcel
*/
public Question (Parcel parcel) {
this.id = parcel.readString();
this.text = parcel.readString();
this.image = parcel.readString();
this.choices = parcel.readArrayList(null);
}
@Override
public int describeContents() {
return 0;
}
// Required method to write to Parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
dest.writeString(image);
dest.writeList(choices);
}
// Method to recreate a Question from a Parcel
public static Creator<Question> CREATOR = new Creator<Question>() {
@Override
public Question createFromParcel(Parcel source) {
return new Question(source);
}
@Override
public Question[] newArray(int size) {
return new Question[size];
}
};
}
How to make generic arraylist parcelable in Android?
I have given up implementing AList
as a Parcelable
. However, since AList
is ArrayList
-like, I can treat it exactly as an ArrayList
when I need to write AList<T>
to Parcel.
For example, I have a AList<Tag> Tags
declared in Food
class. So in protected Food(Parcel in)
, I can write:
ArrayList<Tag> tags = new ArrayList<>();
in.readTypedList(tags, Tag.CREATOR);
Tags = new AList<>(tags);
Of course, I have implemented Tag
as a Parcelable
. And I can have such writeToParcel
:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
dest.writeString(ImagePath);
dest.writeTypedList(Tags.ToArrayList());
dest.writeString(Note);
dest.writeByte((byte) (IsFavorite ? 1 : 0));
if (DateAdded == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeLong(DateAdded);
}
}
My source code for Food
class is here.
Android: How do I correctly implement Parcelable for Objects with ArrayListT with custom types?
Replace the constructor that takes Parcel parameter with this:
public AddresseDetails(Parcel source) {
readFromParcel(source);
}
Your existing constructor is not reading the object members from the parcel in the same order they are written.
This is my test case. I'll experiment more. Maybe storing in a bundle doesn't invoke all the Parcel operations:
OpeningPeriod op = new OpeningPeriod(111, "close", 222, "open");
ArrayList<OpeningPeriod> periods = new ArrayList<>(3);
periods.add(op);
ArrayList<String> weekDays = new ArrayList<>(3);
weekDays.add("aaa");
weekDays.add("bbb");
weekDays.add("ccc");
AddresseDetails ad = new AddresseDetails("number", "name", "ort", periods, "phone",
"place", "plz", "strasse", "web", weekDays);
Bundle b = new Bundle();
b.putParcelable("test", ad);
AddresseDetails ad2 = b.getParcelable("test");
Log.i("Test", "Phone=" + ad.phone + " " + ad2.phone);
Log.i("Test", "WeekDay1=" + ad.weekDayText.get(1) + " " + ad2.weekDayText.get(1));
Parcelable with List only deserialises first element
So this is the answer: My Data parcelable misses the location element when it creates the parcel. This obviously results in some kind of offset error when READING occurs. So the coded solution is as follows:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(uuid.toString());
dest.writeLong(databaseId);
dest.writeLong(createdDate.getTime());
dest.writeLong(modifiedDate.getTime());
dest.writeString(location); /*HERE!*/
dest.writeString(name);
dest.writeString(serial);
}
I hope this helps someone else.
Related Topics
How Apply CSS on a <H:Inputtext>
Add Custom CSS to HTML Code with Jsoup
Different War Files, Shared Resources
Javafx CSS Error ( Property Stylesheets Does Not Exist )
Javafx - How to Create a Thin Menubar
Javafx - What Is This Weird Container That Holds Context Menu
Prevent Android Activity Dialog from Closing on Outside Touch
Android: Pass Data(Extras) to a Fragment
Exposed Beyond App Through Clipdata.Item.Geturi
How to Check Available Space on Android Device? on Sd Card
How Does Facebook Add Badge Numbers on App Icon in Android
Android - How to Take Screenshot Programmatically
How to Fix Google Cloud Messaging Registration Error: Service_Not_Available