android - Linking Two Different Objects For ArrayList -
i have 2 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 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 link them can use them in arraylist.
basically have list of:
driver + vehicle
not sure if should have superclass? because i've read can't this:
arraylist<driver,vehicle> drivers_vehicles = new arraylist<driver,vehicle>
i need sort list
driver_number in driver class.
how can accomplish this?
thanks..
you should make new type uniting both driver , 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 make list of new type , sort this
list<drivervehiclepair> list = new arraylist<drivervehiclepair>(); // fill list data here (with proper arguments in constructors - don't send data below list.add(new drivervehiclepair(new driver(), new vehicle())); // , sort collections.sort(list);
you might want take read on comparable interface
Comments
Post a Comment