I have a problems getting parameters using parcelables. When I call getIntent().getExtras().getParcelable("map"); I have a problem because of a problem with a Vector.
I have 2 parcelable classes:
public class Point implements Parcelable{
private double x;
private double y;
private double z;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public Point(Parcel in ) {
readFromParcel( in );
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Point createFromParcel(Parcel in ) {
return new Point( in );
}
public Point[] newArray(int size) {
return new Point[size];
}
};
public void readFromParcel(Parcel in){
x = in.readDouble();
y = in.readDouble();
z = in.readDouble();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeDouble(x);
dest.writeDouble(y);
dest.writeDouble(z);
}
}
And Map:
public class Map implements Parcelable{
private Point start;
private Point finish;
private java.util.Vector <Point> travel;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public Map(Parcel in ) {
readFromParcel( in );
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Map createFromParcel(Parcel in ) {
return new Map( in );
}
public Map[] newArray(int size) {
return new Map[size];
}
};
public void readFromParcel(Parcel in){
start = in.readParcelable(Point.class.getClassLoader());
finish = in.readParcelable(Point.class.getClassLoader());
Point[] travel = (Point[]) in.readParcelableArray(Point.class.getClassLoader());
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(start, flags);
dest.writeParcelable(finish, flags);
dest.writeParcelableArray(travel.toArray(new Point[travel.size()]), flags);
}
}
Then, how can I receive travel? I always have problems in readParcelableArray.
Someone could help me please??
When is almost impossible to find a solution often the problem is the base of this solution.
In my case, the solution consists in using a List instead of a Vector.
Then, in the class Map, I changed the methods readFromParceo and writeToParcel like these:
public void readFromParcel(Parcel in){
start = in.readParcelable(Point.class.getClassLoader());
finish = in.readParcelable(Point.class.getClassLoader());
travel = in.readArrayList(Point.class.getClassLoader());
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(start, flags);
dest.writeParcelable(finish, flags);
dest.writeList(travel);
}
I hope it'll help somebody in the future.
Related
I got a JSON with an array of doubles and another for Strings. I am trying to create a Parcelable model class with the relevant methods. When the methods are created automaticaly, no line for the doubles' array list is created.
I looked for relevant questions and information, but, surprisingly, couldn't find any. I also added a jar file that was supposed to assist: android-parcelable-intellij-plugin.jar. Don't know what it's supposed to do and couldn't find information on how to use it.
My question is what should I write in the methods in order to get the array of doubles in a List.
The code:
public class Country implements Parcelable {
...
private List<String> timezones;
private List<Double> latlng;
protected Country(Parcel in) {
timezones = in.createStringArrayList();
this.latlng = new ArrayList<>(); // from jsonschema2pojo.org
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(timezones);
//nothing for the double array
}
public List<Double> getLatlng() {
return latlng;
}
public void setLatlng(List<Double> latlng) {
this.latlng = latlng;
}
public List<String> getTimezones() {
return timezones;
}
public void setTimezones(List<String> timezones) {
this.timezones = timezones;
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
}
...
Thanks.
Try this below, it works for me :
public class Country implements Parcelable {
private List<String> timezones;
private List<Double> latlng;
public Country(List<String> timezones, List<Double> latlng) {
this.timezones = timezones;
this.latlng = latlng;
}
protected Country(Parcel in) {
timezones = in.createStringArrayList();
double[] doubleArray = in.createDoubleArray();
latlng = new ArrayList<>();
if (doubleArray != null) {
for (double ele : doubleArray) {
latlng.add(ele);
}
}
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(timezones);
double[] doubleArray = new double[latlng == null ? 0 : latlng.size()];
for (int i=0, len=latlng.size(); i<len; i++) {
doubleArray[i] = latlng.get(i);
}
dest.writeDoubleArray(doubleArray);
}
#Override
public String toString() {
return "Country{" +
"timezones=" + timezones +
", latlng=" + latlng +
'}';
}
}
This code works but I do not pass the data contained in the arrayList.
public class Percorso implements Parcelable {
int id;
String nome;
double distanza;
int numero_commenti;
double valutazione;
ArrayList<Commento> commenti;
ArrayList<Cordinate> cordinate;
public Percorso(){
}
protected Percorso(Parcel in) {
id = in.readInt();
nome = in.readString();
distanza = in.readDouble();
numero_commenti = in.readInt();
valutazione = in.readDouble();
}
public static final Creator<Percorso> CREATOR = new Creator<Percorso>() {
#Override
public Percorso createFromParcel(Parcel in) {
return new Percorso(in);
}
#Override
public Percorso[] newArray(int size) {
return new Percorso[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public double getDistanza() {
return distanza;
}
public void setDistanza(double distanza) {
this.distanza = distanza;
}
public int getNumero_commenti() {
return numero_commenti;
}
public void setNumero_commenti(int numero_commenti) {
this.numero_commenti = numero_commenti;
}
public double getValutazione() {
return valutazione;
}
public void setValutazione(double valutazione) {
this.valutazione = valutazione;
}
public ArrayList<Commento> getCommenti() {
return commenti;
}
public void setCommenti(ArrayList<Commento> commenti) {
this.commenti = commenti;
}
public ArrayList<Cordinate> getCordinate() {
return cordinate;
}
public void setCordinate(ArrayList<Cordinate> cordinate) {
this.cordinate = cordinate;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(nome);
parcel.writeDouble(distanza);
parcel.writeInt(numero_commenti);
parcel.writeDouble(valutazione);
}
}
//
public class Cordinate implements Parcelable {
double latitudine;
double longitudin;
public Cordinate(){}
protected Cordinate(Parcel in) {
latitudine = in.readDouble();
longitudin = in.readDouble();
}
public static final Creator<Cordinate> CREATOR = new Creator<Cordinate>() {
#Override
public Cordinate createFromParcel(Parcel in) {
return new Cordinate(in);
}
#Override
public Cordinate[] newArray(int size) {
return new Cordinate[size];
}
};
public double getLatitudine() {
return latitudine;
}
public void setLatitudine(double latitudine) {
this.latitudine = latitudine;
}
public double getLongitudin() {
return longitudin;
}
public void setLongitudin(double longitudin) {
this.longitudin = longitudin;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeDouble(latitudine);
parcel.writeDouble(longitudin);
}
}
INTENT
Percorso percorsoClick = listaPercorsi.get(i);
Intent intent = new Intent(getBaseContext(), PercorsoActivity.class);
intent.putExtra("percorso", percorsoClick);
startActivity(intent);
GET INTENT
percorso = getIntent().getParcelableExtra("percorso");
System.out.println(percorso.getNome());
getNome() work because is not a arrayList.
instead get getCordinate not work.
percorso.getCordinate().size() // null
I hope it was clear. I have set the Parcelable implementation on all classes.
Thank you very much for the help
ArrayList<Cordinate> cordinate; is not being parcelled. You still have to take care of write/read in/from Parcel parcel. Eg
protected Percorso(Parcel in) {
id = in.readInt();
nome = in.readString();
distanza = in.readDouble();
numero_commenti = in.readInt();
valutazione = in.readDouble();
cordinate = new ArrayList<Cordinate>();
in.readList(cordinate,Cordinate.class.getClassLoader());
}
and
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(nome);
parcel.writeDouble(distanza);
parcel.writeInt(numero_commenti);
parcel.writeDouble(valutazione);
parcel.writeList(cordinate);
}
I've been encountering a strange ClassNotFoundException when unmarshalling Custom Class. This happens at
this.moon = in.readParcelable(Moon.class.getClassLoader());
What is strange is that it happens only on certain devices like Samsung and HTC. Any help will be greatly appreciated. Thank you.
My code snippet is below
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeParcelable(this.warnings, flags);
dest.writeTypedList(this.conditions);
dest.writeParcelable(this.districtForecasts, flags);
dest.writeParcelable(this.localForecasts, flags);
dest.writeParcelable(this.partDayForecasts, flags);
dest.writeParcelable(this.dailyExtremes, flags);
dest.writeParcelable(this.dailyObservations, flags);
dest.writeTypedList(this.historicalObservations);
dest.writeTypedList(this.images);
dest.writeParcelable(this.forecastRainDates, flags);
dest.writeParcelable(this.wavesModel,flags);
dest.writeParcelable(this.tides, flags);
dest.writeTypedList(this.scripts);
dest.writeParcelable(this.animator, flags);
dest.writeParcelable(this.almanac, flags);
dest.writeParcelable(this.marineForecast, flags);
dest.writeParcelable(this.moon, flags);
dest.writeString(this.tag);
dest.writeParcelable(this.snow,flags);
}
protected LocalWeather(Parcel in) {
super(in);
this.warnings = in.readParcelable(Warnings.class.getClassLoader());
this.conditions = in.createTypedArrayList(Condition.CREATOR);
this.districtForecasts = in.readParcelable(DistrictForecasts.class.getClassLoader());
this.localForecasts = in.readParcelable(LocalForecasts.class.getClassLoader());
this.partDayForecasts = in.readParcelable(PartDayForecasts.class.getClassLoader());
this.dailyExtremes = in.readParcelable(DailyExtremes.class.getClassLoader());
this.dailyObservations = in.readParcelable(DailyObservations.class.getClassLoader());
this.historicalObservations = in.createTypedArrayList(Condition.CREATOR);
this.images = in.createTypedArrayList(Images.CREATOR);
this.forecastRainDates = in.readParcelable(RainForecasts.class.getClassLoader());
this.wavesModel = in.readParcelable(Waves.class.getClassLoader());
this.tides = in.readParcelable(Tides.class.getClassLoader());
this.scripts = in.createTypedArrayList(Script.CREATOR);
this.animator = in.readParcelable(Animator.class.getClassLoader());
this.almanac = in.readParcelable(Almanac.class.getClassLoader());
this.marineForecast = in.readParcelable(MarineForecast.class.getClassLoader());
this.moon = in.readParcelable(Moon.class.getClassLoader());
this.tag = in.readString();
this.snow = in.readParcelable(Snow.class.getClassLoader());
}
My Moon class is below
public class Moon implements Parcelable {
List<MoonPhase> phases = new ArrayList<>();
public List<MoonPhase> getPhases() {
return phases;
}
public void setPhases(List<MoonPhase> phases) {
this.phases = phases;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.phases);
}
public Moon() {
}
protected Moon(Parcel in) {
this.phases = in.createTypedArrayList(MoonPhase.CREATOR);
}
public static final Parcelable.Creator<Moon> CREATOR = new Parcelable.Creator<Moon>() {
#Override
public Moon createFromParcel(Parcel source) {
return new Moon(source);
}
#Override
public Moon[] newArray(int size) {
return new Moon[size];
}
};
public MoonPhase getNextPhase() {
if (phases != null && phases.size() > 0) {
return phases.get(0);
} else {
return null;
}
}
}
For List you should use this way :
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this.phases);
}
protected Moon(Parcel in) {
this.phases = new ArrayList<>();
in.readList(this.phases , MoonPhase.class.getClassLoader());
}
The point is using writeListinstead of writeTypedList
It's one day work with no solution,
I would to passed data beetween activity, I implement Parcelable but from called activity I receive the ListFileMedia with a size=0.
When I mistake?
Class ListFileMedia (Extended of ArrayList)
public class ListFileMedia extends ArrayList<FileMedia> implements Parcelable{
public ListFileMedia(){.....}
// for test i have cancel all methods and var, sure i tested also with...
//Interface Parcelable
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
//dest.writeInt(countIntestazioni);
// Add inner class
dest.writeParcelable(this.add(FileMedia),flags);
}
public ListFileMedia(Parcel in) {
}
public final static Parcelable.Creator <ListFileMedia>CREATOR = new Creator<ListFileMedia>() {
#Override
public ListFileMedia createFromParcel(Parcel source) {
return new ListFileMedia(source);
}
#Override
public ListFileMedia[] newArray(int size) {
return new ListFileMedia[size];
}
};
}
Custom objects in FileListMedia
public class FileMedia implements Comparable,Parcelable{
public String getPath() {return path;}
public String getBucket() {
return bucket;
}
public String getMimeType() { return mimeType; }
private String path;
private String bucket;
private String mimeType;
private int giorno;
private int mese;
private int anno;
public FileMedia(int giorno,int mese,int anno, String path,String bucket,String mimeType){
....
}
public int compareTo(Object o) {
.....
}
//Interface Parcelable
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.path);
dest.writeString(this.bucket);
dest.writeString(this.mimeType);
dest.writeInt(this.giorno);
dest.writeInt(this.mese);
dest.writeInt(this.anno);
}
public FileMedia(Parcel in) {
this.path = in.readString();
this.bucket=in.readString();
this.mimeType=in.readString();
this.giorno=in.readInt();
this.mese=in.readInt();
this.anno=in.readInt();
}
public final static Parcelable.Creator <FileMedia>CREATOR = new Creator<FileMedia>() {
#Override
public FileMedia createFromParcel(Parcel source) {
return new FileMedia(source);
}
#Override
public FileMedia[] newArray(int size) {
return new FileMedia[size];
}
};}
Call activity
ListFileMedia test=new ListFileMedia();
ArrayList arrayList=new ArrayList();
test.add(new FileMedia(10,12,2016,"t","t","t"));
b.putParcelable("list",test);
intent.putExtras(b);
startActivity(intent);
Called activity
Intent intent = this.getIntent();
Bundle b=intent.getExtras();
ListFileMedia listFileMedia (ListFileMedia)b.getParcelable("list"); //size==0 !!!
But if i passed only b.putParcelable("list",new FileMedia(10,12,2016,"t","t","t") work correctely!!
Sorry for my English..
I want to pass a custom object between my activities. I implemented Parceable for this class and put it in a extra. It works like a charm. Except if I put some other extras. In this case the app crash with this error : Unmarshalling unknown type code.
I have 2 class : A and B. A has a private field B. et my class B has a nested enum Enum. I implemented Parceable for A, B and Enum.
There is my code, I have rewritten it to be more concise.
A.java
public class A implements Parcelable {
B objectB;
String myString;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(myString);
out.writeParcelable(objectB, flags);
}
public static final Parcelable.Creator<Adresse> CREATOR = new Parcelable.Creator<Adresse>() {
public A createFromParcel(Parcel in) {
return new Adresse(in);
}
public A[] newArray(int size) {
return new A[size];
}
};
private Adresse(Parcel in) {
myString = in.readString();
objectB = in.readParcelable(B.class.getClassLoader());
}
}
B.java
public class B implements Parceable {
Bitmap bitmap;
String otherString;
Enum enum;
public enum Enum implements Parceable {
ONE ("One")
TWO ("Two")
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(ONE.name);
out.writeString(TWO.name);
}
public static final Parcelable.Creator<Identifiant> CREATOR = new Parcelable.Creator<Enum>() {
public Enum createFromParcel(Parcel in) {
return Enum.values()[in.readInt()];
}
public Enum[] newArray(int size) {
return new Enum[size];
}
};
}
public B(Parcel in) {
readFromParcel(in);
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(otherString);
out.writeParcelable(bitmap, flags);
out.writeParcelable(enum, flags);
}
private void readFromParcel(Parcel in) {
otherString = in.readString();
bitmap = in.readParcelable(Bitmap.class.getClassLoader());
enum = in.readParcelable(Enum.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
#Override
public int describeContents() {
return 0;
}
MyActivity.java
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
A objectA = new A();
bundle.putParcelable("A", a);
bundle.putInt("Int", 1); //If I comment this line it works
intent.putExtras(bundle);
startActivityForResult(intent, 1);