Parcelable implementation with field ArrayList<String[]> - java

Currently I use writeList() and readList(), my program is not stable, don't know if this is the problem. This does work at least most times. Same question was asked before here Write Parcelable for ArrayList<String []> in android?, but no satisfactory answer was provided.
public static final class MyParcelable implements Parcelable {
private ArrayList<String []> t_values;
#Override
public int describeContents() {
return hashCode();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(t_values);
};
private MyParcelable(Parcel in) {
t_values = new ArrayList<String[]>();
t_values.readList(in);
}
.....
}

Related

Parcelable.CREATOR on abstract class

I'm trying to pass an ArrayList of unknown class type that extend an abstract class, to another activity using Parcelable. Since its not possible to use Parcelable.CREATOR on the abstract class, there is an error when I try to create the ArrayList: in.readTypedList(AbstractChannel.CREATOR), see below:
public class TvNetwork implements Parcelable {
public String name;
public ArrayList<? extends AbstractChannel> mChannels;
public TvNetwork(String name, ArrayList<? extends AbstractChannel> channels) {
this.name = name;
this.mChannels = channels;
}
protected TvNetwork(Parcel in) {
name = in.readString();
mChannels = in.readTypedList(AbstractChannel.CREATOR); // here is the error
}
public static final Creator<TvNetwork> CREATOR = new Creator<TvNetwork>() {
#Override
public TvNetwork createFromParcel(Parcel in) {
return new TvNetwork(in);
}
#Override
public TvNetwork[] newArray(int size) {
return new TvNetwork[size];
}
};
public ArrayList<? extends AbstractChannel> getChannels() {
return mChannels;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeTypedList(mChannels);
}
}
Writing seems to work but not reading. This obviously does not work either, but explains a bit more what I want to do:
in.readTypedList(mChannels, <? extends AbstractChannel>.class.getClassLoader());
Any ideas?

Using Parcelable Interface for Pojo

for my android app i want to use the parcelable interface to save the data when the app is in brackground. I have 3 Float-Values in my class.
It works but when i wake up my app, the values are mixed. That means the value of the third float attribute is now in the first and so on.
Here is my Pojo Lunch.java:
public class Lunch implements Parcelable {
private String mName;
private float priceStud;
private float priceEmp;
private float priceGuest;
private ArrayList<Lunch> m = new ArrayList<Lunch>();
public static final int PRICE_STUDENT = 0;
public static final int PRICE_EMPLOYEE = 1;
public static int PRICE_GUEST = 2;
public Lunch(Parcel in) {
setmName(in.readString());
setPriceStud(in.readFloat());
setPriceEmp(in.readFloat());
setPriceGuest(in.readFloat());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getmName());
dest.writeFloat(getPriceGuest());
dest.writeFloat(getPriceEmp());
dest.writeFloat(getPriceStud());
}
public static final Creator<Lunch> CREATOR = new Creator<Lunch>() {
public Lunch createFromParcel(Parcel in) {
return new Lunch(in);
}
public Lunch[] newArray(int size) {
return new Lunch[size];
}
};
As Durandal said, you need to specify the exact order in which you want to retrieve them when you access the parcel. I think this is what would solve the problem in your case.
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getmName());
dest.writeFloat(getPriceStud());
dest.writeFloat(getPriceEmp());
dest.writeFloat(getPriceGuest());
}

Read and write an ArrayList of ArrayLists containing Parcelable Objects to pass in an Intent

I just changed all serializable classes in my project to parcelable classes.
Everything is working fine, except for that one class which is made of an ArrayList containing another ArrayList. I already debugged.
There is no error when writing this ArrayList. But I get an error while reading it.
This is the class where the error happens:
public class Timetable implements Parcelable
{
private int actLap = 0;
private ArrayList<Lap> timetable;
private ArrayList<Location> loggedLocations;
private Date startTime; ...
...
public void writeToParcel(Parcel out, int flags)
{
out.writeInt(actLap);
out.writeTypedList(timetable); //no error here
out.writeTypedList(loggedLocations); ...
...
private Timetable(Parcel in)
{
actLap = in.readInt();
in.readTypedList(timetable, Lap.CREATOR); //error after this line
in.readTypedList(loggedLocations, Location.CREATOR);
startTime = (Date)in.readSerializable();
bestLap = in.readParcelable(Lap.class.getClassLoader());
track = in.readParcelable(Track.class.getClassLoader());
description = in.readString();
}
Here are the other classes:
public class Lap implements Parcelable
{
private ArrayList<Time> sectorTimes = new ArrayList<Time>();
private Time laptime; ...
...
public void writeToParcel(Parcel out, int flags)
{
out.writeTypedList(sectorTimes);
out.writeParcelable(laptime,flags);
}
public static final Parcelable.Creator<Lap> CREATOR = new Parcelable.Creator<Lap>()
{
public Lap createFromParcel(Parcel in)
{
return new Lap(in);
}
public Lap[] newArray(int size)
{
return new Lap[size];
}
};
private Lap(Parcel in)
{
in.readTypedList(sectorTimes, Time.CREATOR);
laptime = in.readParcelable(Time.class.getClassLoader());
}
And this class:
public class Time implements Parcelable
{
private long timeLong;
private String timeString;...
...
public void writeToParcel(Parcel out, int flags)
{
out.writeLong(timeLong);
out.writeString(timeString);
}
public static final Parcelable.Creator<Time> CREATOR = new Parcelable.Creator<Time>()
{
public Time createFromParcel(Parcel in)
{
return new Time(in);
}
public Time[] newArray(int size)
{
return new Time[size];
}
};
private Time(Parcel in)
{
timeLong = in.readLong();
timeString = in.readString();
}
Like I already said, everything works fine (there are more Parcelable classes which I pass with intents (including a single ArrayList) and which I save in files).
So can you guys help me to write and read that double ArrayList?
Thanks in advance
If your code is as posted, then you're just missing the initialization of the ArrayLists, i.e.
private Timetable(Parcel in)
{
// readTypeList() needs an existing List<> to load.
timetable = new ArrayList<Lap>();
loggedLocations = new ArrayList<Location>();
actLap = in.readInt();
in.readTypedList(timetable, Lap.CREATOR);
in.readTypedList(loggedLocations, Location.CREATOR);
...

Help with creating a Parcelable class

I am trying to create a Parcelable class. The class extends TableRow:
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.TableRow;
public class AcmTableRow extends TableRow implements Parcelable{
private int index;
public boolean isSection;
private String volumeLink;
private String rowId;
private String cfr;
private static Context context;
public AcmTableRow(Context context) {
super(context);
}
public AcmTableRow(Context context, Parcel in) {
super(context);
AcmTableRow.context = context;
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
//strValue = in.readString();
//intValue = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
//dest.writeString(strValue);
//dest.writeInt(intValue);
}
public void setIndex(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
public void setRowId(String rowId) {
this.rowId = rowId;
}
public String getRowId() {
return rowId;
}
public void setCfr(String cfr) {
this.cfr = cfr;
}
public String getCfr() {
return cfr;
}
public void setVolumeLink(String volumeLink) {
this.volumeLink = volumeLink;
}
public String getVolumeLink() {
return volumeLink;
}
public static final Parcelable.Creator<AcmTableRow> CREATOR = new Parcelable.Creator<AcmTableRow>() {
public AcmTableRow createFromParcel(Parcel in) {
return new AcmTableRow(context, in);
}
public AcmTableRow[] newArray(int size) {
return new AcmTableRow[size];
}
};
}
I am confused as what I need to put in:
readFromParcel(Parcel in)
And
writeToParcel(Parcel dest, int flags)
Any explanation or help is greatly appreciated.
Thank you
Phil
The writeToParcel method simply flattens the object into a parcel. You'll use this when you need to pass your object between activities. In your case, it should look like this:
public void writeToParcel (Parcel dest, int flags)
{
dest.writeInt(index);
dest.writeBoolean(isSection);
dest.writeString(volumeLink);
dest.writeString(rowId);
dest.writeString(cfr);
}
I've never had to use readFromParcel and I'm not sure you need to here either. Your object will be created from the Parcel when you call the appropriate method in your next activity.
Just a quick note, you probably don't want to pass the context in your parcelable class. I'm not even sure you can. You'll need to assign the context when you inflate the parcelable again later on.
This is a simple working Parcelable example:
public class ParcelableExample implements Parcelable {
private int intMember;
private String stringMember;
/*
* Getters and setters
* ....
*/
/*
* Constructors
* ....
*/
/*
* Custom parcelable implementation
*/
private ParcelableExample(Parcel in) {
// notice that we are reading in the same order as we have written
intMember = in.readInt();
stringMember = in.readString();
}
#Override
public int describeContents() {
return hashCode();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(intMember);
dest.writeString(stringMember);
}
#SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public ParcelableExample createFromParcel(Parcel in) {
return new ParcelableExample(in);
}
public ParcelableExample[] newArray(int size) {
return new ParcelableExample[size];
}
}

Arraylist in parcelable object

I have seen many parcelable examples so far, but for some reason I can't get it to work when it gets a bit more complex.
I have a Movie object, which implements Parcelable. This book object contains some properties, such as ArrayLists.
Running my app results in a NullPointerException when executing the ReadTypedList ! I'm really out of ideas here
public class Movie implements Parcelable{
private int id;
private List<Review> reviews
private List<String> authors;
public Movie () {
reviews = new ArrayList<Review>();
authors = new ArrayList<String>();
}
public Movie (Parcel in) {
readFromParcel(in);
}
/* getters and setters excluded from code here */
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeList(reviews);
dest.writeStringList(authors);
}
public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
public MoviecreateFromParcel(Parcel source) {
return new Movie(source);
}
public Movie[] newArray(int size) {
return new Movie[size];
}
};
/*
* Constructor calls read to create object
*/
private void readFromParcel(Parcel in) {
this.id = in.readInt();
in.readTypedList(reviews, Review.CREATOR); /* NULLPOINTER HERE */
in.readStringList(authors);
}
}
The Review class:
public class Review implements Parcelable {
private int id;
private String content;
public Review() {
}
public Review(Parcel in) {
readFromParcel(in);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(content);
}
public static final Creator<Review> CREATOR = new Creator<Review>() {
public Review createFromParcel(Parcel source) {
return new Review(source);
}
public Review[] newArray(int size) {
return new Review[size];
}
};
private void readFromParcel(Parcel in) {
this.id = in.readInt();
this.content = in.readString();
}
}
I would be very grateful if someone could just get me on the right track, I have spend quite a bit of time searching for this one !
Thanks in adnvance
Wesley
reviews and authors are both null. You should first initialize the ArrayList. One way to do this is chain the constructor:
public Movie (Parcel in) {
this();
readFromParcel(in);
}
From the javadocs for readTypedList:
Read into the given List items containing a particular object type
that were written with writeTypedList(List)
at the current dataPosition(). The list must have previously been written via writeTypedList(List) with the same object type.
You wrote them with a plain
dest.writeList(reviews);

Categories

Resources