0

我有两节课:

司机等级:

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];
            }
        };
    }

和车辆类别:

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];
            }
        };
    }

我正在尝试将它们链接在一起,以便可以在 ArrayList 中使用它们。

基本上有一个清单:

司机+车辆

不确定我是否应该有一个超类?因为我读过我们不能做这样的事情:

ArrayList<Driver,Vehicle> drivers_vehicles = new ArrayList<Driver,Vehicle>

我将需要Sort the list通过 Driver 类中的 driver_number 。

我怎样才能做到这一点?

谢谢..

4

1 回答 1

2

您应该制作一种将驾驶员和车辆结合在一起的新型。IE

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;
    }

    ...
}

然后你列出你的新类型并像这样排序

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);

您可能想阅读Comparable interface

于 2013-07-11T06:55:43.427 回答