为ArrayList链接两个不同的对象

时间:2013-07-11 06:40:20

标签: android

我有两个班级:

驱动程序类:

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>

我需要在Driver类中通过driver_number Sort the list

我该如何做到这一点?

谢谢..

1 个答案:

答案 0 :(得分:2)

你应该制作一个新的类型,将驾驶员和车辆联系起来。即。

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