readParcelable returns null - android

I try to pass a News class to an activity, so I implemented the Parcelable interface. Inside News I have two classes implementing Parcelable too, Image and Date
The matter is that my News object at the end contains null for fields image and date.
Here my code:
News.java
public class News implements Parcelable {
public static final String TAG = "model_news";
private JSONObject object;
private int id;
private String type;
private String title;
private Boolean comment_disabled;
private String category_name;
private String url;
private Image images;
private Date date;
private Boolean is_video;
public News(JSONObject object) {
this.object = object;
try {
id = Integer.parseInt(object.getString("id"));
type = object.getString("type");
title = object.getString("title");
comment_disabled = object.getBoolean("comment_disabled");
category_name = object.getString("category_name");
url = object.getString("url");
if (!object.isNull("images")) {
images = new Image(object.getJSONObject("images"));
}
date = new Date(object.getJSONObject("date"));
is_video = object.getBoolean("is_video");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
}
protected News(Parcel in) {
id = in.readInt();
type = in.readString();
title = in.readString();
category_name = in.readString();
url = in.readString();
images = (Image) in.readParcelable(Image.class.getClassLoader());
date = (Date) in.readParcelable(Date.class.getClassLoader());
is_video = in.readByte() != 0;
comment_disabled = in.readByte() != 0;
}
public static final Creator<News> CREATOR = new Creator<News>() {
#Override
public News createFromParcel(Parcel in) {
return new News(in);
}
#Override
public News[] newArray(int size) {
return new News[size];
}
};
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(type);
dest.writeString(title);
dest.writeByte((byte) (comment_disabled ? 1 : 0));
dest.writeString(category_name);
dest.writeString(url);
dest.writeParcelable(images, flags);
dest.writeParcelable(date, flags);
dest.writeByte((byte) (is_video ? 1 : 0));
}
#Override
public int describeContents() {
return 0;
}
}
Image.java
public class Image implements Parcelable {
public static final String TAG = "model_image";
private JSONObject imageObj;
private JSONObject original;
private String source;
private int width;
private Drawable image;
public Image(JSONObject imageObj) {
this.imageObj = imageObj;
try {
original = this.imageObj.getJSONObject("original");
source = original.getString("src");
width = original.getInt("width");
} catch (JSONException e) {
e.getMessage();
}
}
protected Image(Parcel in) {
source = in.readString();
width = in.readInt();
}
public static final Creator<Image> CREATOR = new Creator<Image>() {
#Override
public Image createFromParcel(Parcel in) {
return new Image(in);
}
#Override
public Image[] newArray(int size) {
return new Image[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(source);
dest.writeInt(width);
}
}
what I'm doing wrong ?

There is mistake in the Parcelable implementation.
First of all parcelable implementation states that: the fields passed in News(Parcel in) Constructor should be written in the same sequence in writeToParcel() method. Thats called Marshalling and Unmarshalling.
Corrections:
Drawable cannot be passed a parameter in Parcelable.
News Parcelable implementation.
Missed some of the fields its just for your understanding.
public class News implements Parcelable {
public static final String TAG = "model_news";
private JSONObject object;
private int id;
private String type;
private String title;
private Boolean comment_disabled;
private String category_name;
private String url;
private Image images;
private Date date;
private Boolean is_video;
protected News(Parcel in) {
id = in.readInt();
type = in.readString();
title = in.readString();
category_name = in.readString();
url = in.readString();
}
public static final Creator<News> CREATOR = new Creator<News>() {
#Override
public News createFromParcel(Parcel in) {
return new News(in);
}
#Override
public News[] newArray(int size) {
return new News[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(type);
dest.writeString(title);
dest.writeString(category_name);
dest.writeString(url);
}
}
public class Image implements Parcelable {
public static final String TAG = "model_image";
private JSONObject imageObj;
private JSONObject original;
private String source;
private int width;
private Drawable image;
protected Image(Parcel in) {
source = in.readString();
width = in.readInt();
}
public static final Creator<Image> CREATOR = new Creator<Image>() {
#Override
public Image createFromParcel(Parcel in) {
return new Image(in);
}
#Override
public Image[] newArray(int size) {
return new Image[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(source);
dest.writeInt(width);
}
}

Related

Pass array in Intent using parcelable

I would like to send an array of objects between activities. I want to use the parcelable interface and send the data in an intent. However I keep getting errors. I have been stuck for 2 days. Here are some details about my problem.
Class A
private ProjetUI[] mProjects;
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Context context = view.getContext();
Intent intent = new Intent(context, ProjetListActivity.class);
intent.putExtra(ProjetListActivity.ARG_PROJECTS, mProjects);
context.startActivity(intent);
}
};
Class B
ProjetUI[] mProjects = getIntent().getParcelableArrayExtra(ARG_PROJECTS);
I get a compilation error "Incompatible types"
After casting to (ProjetUI[]), I get a runtime error "Cannot cast Parcelable[] to ProjetUI[]"
Class Projet
public class ProjetUI implements Parcelable {
private String id;
private String idParent;
private String nom;
private String description;
private List<ProjetColonneUI> colonnes;
private List<VueUI> vues;
private boolean archive;
private String version;
private String commentaire;
private boolean published;
private List<DroitAccesUI> droitAcces;
private String idDossier;
private String typeDossier;
private String idModele;
private List<ProjetDatasetUI> projetDatasets;
protected ProjetUI(Parcel in) {
id = in.readString();
idParent = in.readString();
nom = in.readString();
description = in.readString();
colonnes = in.createTypedArrayList(ProjetColonneUI.CREATOR);
vues = in.createTypedArrayList(VueUI.CREATOR);
archive = in.readInt() == 1;
version = in.readString();
commentaire = in.readString();
published = in.readInt() == 1;
droitAcces = in.createTypedArrayList(DroitAccesUI.CREATOR);
idDossier = in.readString();
typeDossier = in.readString();
idModele = in.readString();
projetDatasets = in.createTypedArrayList(ProjetDatasetUI.CREATOR);
}
public static final Creator<ProjetUI> CREATOR = new Creator<ProjetUI>() {
#Override
public ProjetUI createFromParcel(Parcel in) {
return new ProjetUI(in);
}
#Override
public ProjetUI[] newArray(int size) {
return new ProjetUI[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(getId());
parcel.writeString(getIdParent());
parcel.writeString(getNom());
parcel.writeString(getDescription());
parcel.writeTypedList(getColonnes());
parcel.writeTypedList(getVues());
parcel.writeInt(isArchive() ? 1 : 0);
parcel.writeString(getVersion());
parcel.writeString(getCommentaire());
parcel.writeInt(isPublished() ? 1 : 0);
parcel.writeTypedList(getDroitAcces());
parcel.writeString(getIdDossier());
parcel.writeString(getTypeDossier());
parcel.writeString(getIdModele());
parcel.writeTypedList(getProjetDatasets());
}
}
EDIT
This is the complete stacktrace
The other classes implement parcelable just like ProjeUI class.
Here is an example of another class that has an enum type and an example of an enum that implements parcelable
public class VueRelationUI implements Parcelable {
private String id;
private String idVue;
private String idRelation;
private RelationType typeRelation;
protected VueRelationUI(Parcel in) {
id = in.readString();
idVue = in.readString();
idRelation = in.readString();
typeRelation = in.readParcelable(RelationType.class.getClassLoader());
}
public static final Creator<VueRelationUI> CREATOR = new Creator<VueRelationUI>() {
#Override
public VueRelationUI createFromParcel(Parcel in) {
return new VueRelationUI(in);
}
#Override
public VueRelationUI[] newArray(int size) {
return new VueRelationUI[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(getId());
parcel.writeString(getIdVue());
parcel.writeString(getIdRelation());
parcel.writeParcelable(getTypeRelation(), flags);
}
}
ENUM
public enum RelationType implements Parcelable {
INNER,
OUTER;
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(ordinal());
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<RelationType> CREATOR = new Creator<RelationType>() {
#Override
public RelationType createFromParcel(Parcel parcel) {
return RelationType.values()[parcel.readInt()];
}
#Override
public RelationType[] newArray(int size) {
return new RelationType[size];
}
};
}
Any help would be much appreciated
The problem happens because of the internal implementation of Android's Parcel class. When you start the new activity, all of the intent extras are parceled and then unparceled. When this happens, the Android framework allocates a new Parcelable[], and not a new ProjetUI[]. So you get a ClassCastException when you try to cast it.
Probably the best solution would be to change your code to use ArrayList<ProjetUI> instead of ProjetUI[]. Then you can use Intent.putParcelableArrayListExtra() and getParcelableArrayListExtra() without any problems.
If you can't do that for some reason, then you will have to manually cast the array one element at a time:
Parcelable[] parcelables = getIntent().getParcelableArrayExtra(ARG_PROJECTS);
ProjetUI[] mProjects = new ProjetUI[parcelables.length];
for (int i = 0; i < parcelables.length; ++i) {
mProjects[i] = (ProjetUI) parcelables[i];
}

Pass ArrayList of Parcelable objects throws error

I have an object with a couple of Strings, one int and another object which has 4 inner objects. All of them implementing Parcelable. In order to generate the boilerplate code, I used Parcelable plugin from Android studio.
Even though all the objects are parcelable, I'm getting the following error: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: AlbumTrackResponse
Here's how the POJOs look like:
Main object:
public class AlbumTrackResponse implements Parcelable {
private String id;
private String albumId;
private String position;
private String title;
private int rate;
private AllServices services;
public AllServices getServices() {
return services;
}
public void setServices(AllServices services) {
this.services = services;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAlbumId() {
return albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
protected AlbumTrackResponse(Parcel in) {
id = in.readString();
albumId = in.readString();
position = in.readString();
title = in.readString();
rate = in.readInt();
services = (AllServices) in.readValue(AllServices.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(albumId);
dest.writeString(position);
dest.writeString(title);
dest.writeInt(rate);
dest.writeValue(services);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<AlbumTrackResponse> CREATOR = new Parcelable.Creator<AlbumTrackResponse>() {
#Override
public AlbumTrackResponse createFromParcel(Parcel in) {
return new AlbumTrackResponse(in);
}
#Override
public AlbumTrackResponse[] newArray(int size) {
return new AlbumTrackResponse[size];
}
};
}
Inner object with 4 objects
public class AllServices implements Parcelable {
private GigRevService gigrev;
private AppleService apple;
private GoogleService google;
private SpotifyService spotify;
protected AllServices(Parcel in) {
gigrev = (GigRevService) in.readValue(GigRevService.class.getClassLoader());
apple = (AppleService) in.readValue(AppleService.class.getClassLoader());
google = (GoogleService) in.readValue(GoogleService.class.getClassLoader());
spotify = (SpotifyService) in.readValue(SpotifyService.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
public GigRevService getGigrev() {
return gigrev;
}
public void setGigrev(GigRevService gigrev) {
this.gigrev = gigrev;
}
public AppleService getApple() {
return apple;
}
public void setApple(AppleService apple) {
this.apple = apple;
}
public GoogleService getGoogle() {
return google;
}
public void setGoogle(GoogleService google) {
this.google = google;
}
public SpotifyService getSpotify() {
return spotify;
}
public void setSpotify(SpotifyService spotify) {
this.spotify = spotify;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(gigrev);
dest.writeValue(apple);
dest.writeValue(google);
dest.writeValue(spotify);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<AllServices> CREATOR = new Parcelable.Creator<AllServices>() {
#Override
public AllServices createFromParcel(Parcel in) {
return new AllServices(in);
}
#Override
public AllServices[] newArray(int size) {
return new AllServices[size];
}
};
}
The inner objects from here contain just string items.
Here's how I pass the ArrayList to the bundle:
bundle.putParcelableArrayList(MediaPlayerService.TRACK_LIST, trackList);
I'm passing the items from an Activity to a Service. Any ideas why I'm getting this error?
EDIT: Implementation of service classes
public class GoogleService implements Parcelable {
private String name;
private String uri;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected GoogleService(Parcel in) {
name = in.readString();
uri = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(uri);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<GoogleService> CREATOR = new Parcelable.Creator<GoogleService>() {
#Override
public GoogleService createFromParcel(Parcel in) {
return new GoogleService(in);
}
#Override
public GoogleService[] newArray(int size) {
return new GoogleService[size];
}
};
}
This looks extremly strange
this.services = in.readParcelable(getClass().getClassLoader());
You are pointing out to the wrong class loader. You have to use it like this
this.services = (AllServices)in.readParcelable(AllServices.class.getClassLoader());
Besides that, I am always using this site http://www.parcelabler.com/ to quickly generate my Parceables. Or you could use it like this
services = (AllServices) in.readValue(AllServices.class.getClassLoader());
Here:
protected AllServices(Parcel in) { this.gigrev = in.readParcelable(getClass().getClassLoader());
this.apple = in.readParcelable(getClass().getClassLoader());
this.google = in.readParcelable(getClass().getClassLoader());
this.spotify = in.readParcelable(getClass().getClassLoader()); }
Replace it with
protected AllServices(Parcel in) { this.gigrev = in.readParcelable(GigRevService.class.getClassLoader());
this.apple = in.readParcelable(AppleService.class.getClassLoader());
this.google = in.readParcelable(GoogleService.class.getClassLoader());
this.spotify = in.readParcelable(SpotifyService.class.getClassLoader()); }
And replace:
this.services = in.readParcelable(getClass().getClassLoader());
With:
this.services = in.readParcelable(AllServices.class.getClassLoader());
Also can you show the GoogleService etc classes? Each inner object's class must have a Parcelable creator also and implement the Parcelable interface as well.

Android Intent Parcelable: Class not found when unmarshalling

I looked all answers to question "Class not found when unmarshalling" in the Stackoverflow, but I don't found solution to my situation. I tried any variants but didn't work.
I get following error:
Here my Classes:
Class one:
public class Class1 implements Parcelable, Serializable {
private static final long serialVersionUID = -3892107077759983950L;
private long id;
private ArrayList<Class2> details;
public Class1(long id, ArrayList<Class2> details) {
this.id = id;
this.details = details;
}
public Class1(Parcel in) {
this.id = in.readLong();
details = new ArrayList<Class2>((Collection<? extends Class2>) Arrays.asList(in.readParcelableArray(Class2.class.getClassLoader())));
// details = (ArrayList<OrderDetail>)in.readSerializable();
// details = in.readArrayList(Class2.class.getClassLoader());
// in.readList(details, Class2.class.getClassLoader());
// in.readTypedList(details, Class2.CREATOR);
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
// dest.writeSerializable(details);
// dest.writeList(details);
// dest.writeTypedList(details);
dest.writeParcelableArray(ordDetailListToArr(), flags);
}
#Override
public int describeContents() { return 0; }
public static final Parcelable.Creator<Class1> CREATOR
= new Parcelable.Creator<Class1>() {
public Class1 createFromParcel(Parcel in) {
return new Class1(in);
}
public Class1[] newArray(int size) {
return new Class1[size];
}
};
}
Class second:
public class Class2 implements Parcelable, Serializable {
private static final long serialVersionUID = 3242734374178052427L;
private long orderId;
private Class3 Class3;
public Class2(long orderId, Class3 Class3) {
this.orderId = orderId;
this.Class3 = Class3;
}
private Class2(Parcel in) {
this.orderId = in.readLong();
this.Class3 = (Class3) in.readParcelable(Class3.class.getClassLoader());
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(orderId);
dest.writeParcelable(Class3, flags);
}
#Override
public int describeContents() { return 0;}
public static final Parcelable.Creator<Class2> CREATOR
= new Parcelable.Creator<Class2>() {
public Class2 createFromParcel(Parcel in) {
return new Class2(in);
}
public Class2[] newArray(int size) {
return new Class2[size];
}
};
}
Class third:
public class Class3 extends Class4 implements Cloneable, Serializable {
private static final long serialVersionUID = 8712386538880552241L;
private int count;
public Class3(Class4 Class4, int count) {
super(Class4.getId(), Class4.getName());
this.count = count;
}
private Class3(Parcel in) {
super(in);
this.count = in.readInt();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(count);
}
#Override
public int describeContents() { return 0;}
public static final Parcelable.Creator<Class3> CREATOR
= new Parcelable.Creator<Class3>() {
public Class3 createFromParcel(Parcel in) {
return new Class3(in);
}
public Class3[] newArray(int size) {
return new Class3[size];
}
};
}
class fourth:
public class Class4 implements Parcelable, Serializable {
private static final long serialVersionUID = -8394588748958519736L;
private long id;
private String name;
public Class4(long id, String name) {
this.id = id;
this.name = name;
}
public Class4(Parcel in) {
this.id = in.readLong();
this.name = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(name);
}
#Override
public int describeContents() { return 0;}
public static final Parcelable.Creator<Class4> CREATOR
= new Parcelable.Creator<Class4>() {
public Class4 createFromParcel(Parcel in) {
return new Class4(in);
}
public Class4[] newArray(int size) {
return new Class4[size];
}
};
}
I used Serializable in all classes for exchange(read/write object) data via Socket channel.
I passed data from my first activity to second activity following way:
First Activity:
Class1 class1 = ...;
Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
orderedProductsActivity.putExtra("mydata", (Parcelable)class1);
startActivity(secondActivity);
Second Activity:
Class1 class1 = (Class1) getIntent().getParcelableExtra("mydata");
I had this issue in the past, in my case it was related with ArrayList, can you try to do:
array = (ArrayList<Long>) in.readSerializable();
and
dest.writeSerializable(array);
instead of using writeParcelableArray

Implementing parcelable in a class to pass it from an activity to another

i have class student
i tried to use seriablizable but couldnt make it work... this didnt work too can any one find whats wrong with it?
that uses class semester and Course in its variables take a look
public class Student implements Parcelable {
private double GPA, MGPA;
private LinkedList<Course> finishedcourses;
private String username, password;
private Semester semester;
public Student(String user, String pass, double gpa, double mgpa,
LinkedList<Course> fc, Semester s) {
username = user;
password = pass;
GPA = gpa;
MGPA = mgpa;
semester = s;
finishedcourses = fc;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(username);
out.writeString(password);
out.writeDouble(GPA);
out.writeDouble(MGPA);
out.writeParcelable((Parcelable) finishedcourses , flags);
out.writeParcelable((Parcelable) semester, flags);
}
public static final Creator<Student> CREATOR = new Parcelable.Creator<Student> (){
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
public Student(Parcel in)
{
username = in.readString();
password = in.readString();
GPA = in.readDouble();
MGPA = in.readDouble();
finishedcourses = in.readParcelable(Course.class.getClassLoader());
semester = in.readParcelable(Semester.class.getClassLoader());
}}
this is class Semester:
public class Semester implements Parcelable{
private LinkedList<Course> current;
private int Credits;
public Semester(LinkedList<Course> current) {
super();
this.current = current;
Credits = calcCredits();
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(Credits);
out.writeParcelable((Parcelable) current, flags);
}
public Semester(Parcel in) {
Credits = in.readInt();
current = in.readParcelable(Course.class.getClassLoader());
}
public static final Creator<Semester> CREATOR = new Parcelable.Creator<Semester>() {
public Semester createFromParcel(Parcel in)
{
return new Semester(in);
}
#Override
public Semester[] newArray(int size) {
// TODO Auto-generated method stub
return new Semester[size];
}
};}
This is class course
public class Course implements Parcelable{
private int credits;
private String LetterGrade,Name;
public Course(int credits, String Name, String Letter) {
this.Name = Name;
LetterGrade = Letter;
this.credits = credits;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(Name);
out.writeString(LetterGrade);
out.writeInt(credits);
}
public static final Creator<Course> CREATOR = new Parcelable.Creator<Course>() {
#Override
public Course createFromParcel(Parcel in) {
return new Course(in);
}
#Override
public Course[] newArray(int size) {
return new Course[size];
}
};
public Course (Parcel in){
Name = in.readString();
LetterGrade = in.readString();
credits = in.readInt();
}}
try this in Student class:
//initialize the list
private LinkedList<Course> finishedcourses = new LinkedList<Course>();
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(username);
out.writeString(password);
out.writeDouble(GPA);
out.writeDouble(MGPA);
//replace by writeTypedList
out.writeTypedList(finishedcourses);
out.writeParcelable(semester, flags);
}
public Student(Parcel in)
{
username = in.readString();
password = in.readString();
GPA = in.readDouble();
MGPA = in.readDouble();
//replace by readTypedList
in.readTypedList(finishedcourses, Course.CREATOR);
semester = in.readParcelable(Semester.class.getClassLoader());
}
Do the same for the list private LinkedList current; in class Semester.

Linking Two Different Objects For ArrayList

I have two classes:
Driver Class:
public static final class Driver implements DriverColumns, BaseColumns, Parcelable {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_DRIVERS).build();
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.test.console.drivers";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.console.drivers";
private int driver_id, driver_number;
private String driver_name;
public Driver() {
}
public Driver(int driverID, int driverNumber, String driverName) {
this.setDriverID(driverID);
this.setDriverNumber(driverNumber);
this.setDriverName(driverName);
}
public static Uri buildDriverUri(String driverID) {
return CONTENT_URI.buildUpon().appendPath(driverID).build();
}
public int getDriverID() {
return driver_id;
}
public void setDriverID(int driver_id) {
this.driver_id = driver_id;
}
public int getDriverNumber() {
return driver_number;
}
public void setDriverNumber(int driver_number) {
this.driver_number = driver_number;
}
public String getDriverName() {
return driver_name;
}
public void setDriverName(String driver_name) {
this.driver_name = driver_name;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(driver_id);
dest.writeInt(driver_number);
dest.writeString(driver_name);
}
private Driver(Parcel in) {
this.driver_id = in.readInt();
this.driver_number = in.readInt();
this.driver_name = in.readString();
}
public static final Parcelable.Creator<Driver> CREATOR = new Parcelable.Creator<Driver>() {
#Override
public Driver createFromParcel(Parcel source) {
return new Driver(source);
}
#Override
public Driver[] newArray(int size) {
return new Driver[size];
}
};
}
and a Vehicle Class:
public static final class Vehicle implements VehicleColumns, BaseColumns, Parcelable {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_VEHICLES).build();
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.test.console.vehicles";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.console.vehicles";
private int vehicle_id, vehicle_number;
private String vehicle_vin;
public Vehicle() {
}
public Vehicle(int vehicleID, int vehicleNumber, String vehicleVin) {
setVehicleID(vehicleID);
setVehicleNumber(vehicleNumber);
setVehicleVin(vehicleVin);
}
public int getVehicleID() {
return vehicle_id;
}
public void setVehicleID(int mVehicleID) {
this.vehicle_id = mVehicleID;
}
public int getVehicleNumber() {
return vehicle_number;
}
public void setVehicleNumber(int mVehicleNumber) {
this.vehicle_number = mVehicleNumber;
}
public String getVehicleVin() {
return vehicle_vin;
}
public void setVehicleVin(String mVehicleVin) {
this.vehicle_vin = mVehicleVin;
}
public static Uri buildVehicleUri(String vehicleID) {
return CONTENT_URI.buildUpon().appendPath(vehicleID).build();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(vehicle_id);
dest.writeInt(vehicle_number);
dest.writeString(vehicle_vin);
}
private Vehicle(Parcel in) {
this.vehicle_id = in.readInt();
this.vehicle_number = in.readInt();
this.vehicle_vin = in.readString();
}
public static final Parcelable.Creator<Vehicle> CREATOR = new Parcelable.Creator<Vehicle>() {
#Override
public Vehicle createFromParcel(Parcel source) {
return new Vehicle(source);
}
#Override
public Vehicle[] newArray(int size) {
return new Vehicle[size];
}
};
}
I'm trying to Link them together so I can use them in an ArrayList.
Basically Have a List of:
Driver + Vehicle
Not sure if I should have a superclass? Because i've read that we can't do something like this:
ArrayList<Driver,Vehicle> drivers_vehicles = new ArrayList<Driver,Vehicle>
I will need to Sort the list by driver_number in the Driver class.
How can I accomplish this?
Thanks..
You should make a new type uniting both driver and vehicle. I.e.
public class DriverVehiclePair implements Comparable<DriverVehiclePair> {
public Driver driver;
public Vehicle vehicle;
public int compareTo(DriverVehiclePair compareObject)
{
if (driver.driver_number < compareObject.driver_number)
return -1;
else if (driver_number == compareObject.driver_number)
return 0;
else
return 1;
}
...
}
Then you make a list of your new type and sort it like this
List<DriverVehiclePair> list = new ArrayList<DriverVehiclePair>();
// fill your list with data here (with proper arguments in constructors - I don't send any data below
list.add(new DriverVehiclePair(new Driver(), new Vehicle()));
// and finally sort it
Collections.sort(list);
You might want to take a read on Comparable interface

Categories

Resources