获取特定表列下的元素列表

时间:2015-02-19 16:20:48

标签: java vaadin

此表格由我的代码生成。 (表的数据是垃圾)

enter image description here

问题: 如何按照它们出现的顺序获取某列下的项目列表?

我想做像table.GetListFromColumn(" carmake")这样的事情,所以我会有["阿斯顿马丁" ,"保时捷911","法拉利"]。

或beanItemContainer.GetListFromColumn(" carmake")?

@Override
public void init(VaadinRequest request) {

    BrakeResponseTime time1 = new BrakeResponseTime(0.2f);
    BrakeResponseTime time2 = new BrakeResponseTime(0.8f);
    BrakeResponseTime time3 = new BrakeResponseTime(0.5f);

    Collection<SportsCar>c = new ArrayList<SportsCar>();
    c.add(new SportsCar("ferrari", 180.0, "Tom", time1));
    c.add(new SportsCar("Aston Martin", 165.0, "Harry", time2));
    c.add(new SportsCar("Porche 911", 145.0, "Dick", time3));

    MyBeanItemContainer container = new MyBeanItemContainer(SportsCar.class,c);

    Table table = new Table();
    t.setContainerDataSource(container);
    this.setContent(table);

}

public class BrakeResponseTime implements Comparable<BrakeResponseTime> {

float time;

/**
 * Create a Demo.BrakeResponseTime.
 */
public BrakeResponseTime(float time) {
    this.time = time;
}

/**
 * Get the time.
 * 
 * @return the time.
 */
public float getTime() {
    return time;
}

/**
 * Set the time.
 * 
 * @param time
 *            the time.
 */
public void setTime(float time) {
    this.time = time;
}

@Override
public String toString() {
    return time +"/%";
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Comparable#compareTo(java.lang.Object)
 */
@Override
public int compareTo(BrakeResponseTime o) {
    if (null == o) {
        return 1;
    }
    if(time==o.getTime()){
        return 0;
    }
    return (time < o.getTime() ? -1 : 1);
}

}

public class MyBeanItemContainer extends BeanItemContainer<SportsCar> {
/**
 * Create a Demo.MyBeanItemContainer.
 */
public MyBeanItemContainer(Class<SportsCar> type, Collection<SportsCar> collection) {
    super(type, collection);
}

private int compareBrakeResponseTimeWithNullCheck(BrakeResponseTime o1, BrakeResponseTime o2) {
    if (null == o1 && (null == o2)) {
        return 0;
    } else if (null == o1 && (null != o2)) {
        return -1;
    }else if(null != o1 && (null == o2)){
        return 1;
    } else {
        return o1.compareTo(o2);
    }
}

@Override
public void sort(final Object[] propertyId, final boolean[] ascending) {
    super.sort(propertyId, ascending);
    System.out.println(Arrays.toString(propertyId));
    final boolean sortAscending = ascending[0];
    final Object sortContainerPropertyId = propertyId[0];
    List<SportsCar> list = super.getAllItemIds();
    Collections.sort(list, new Comparator<SportsCar>() {
        @Override
        public int compare(final SportsCar o1, final SportsCar o2) {
            int result = 0;
            if ("responseTime".equals(sortContainerPropertyId)) {
                result = compareBrakeResponseTimeWithNullCheck(o1.getResponseTime(), o2.getResponseTime());
            }

            if (!sortAscending) {
                result *= -1;
            }
            return result;
        }
    });
}
}



public class SportsCar {
String carMake;
double topspeed;
String driver;
BrakeResponseTime responseTime;

public SportsCar(String carMake, double topspeed, String driver, BrakeResponseTime responseTime) {

    this.carMake = carMake;
    this.topspeed = topspeed;
    this.driver = driver;
    this.responseTime = responseTime;
}

/**
 * Get the carMake.
 * 
 * @return the carMake.
 */
public String getcarMake() {
    return carMake;
}

/**
 * Set the carMake.
 * 
 * @param carMake
 *            the carMake.
 */
public void setcarMake(String carMake) {
    this.carMake = carMake;
}

/**
 * Get the topspeed.
 * 
 * @return the topspeed.
 */
public double getTopspeed() {
    return topspeed;
}

/**
 * Set the topspeed.
 * 
 * @param topspeed
 *            the topspeed.
 */
public void setTopspeed(double topspeed) {
    this.topspeed = topspeed;
}

/**
 * Get the driver.
 * 
 * @return the driver.
 */
public String getDriver() {
    return driver;
}

/**
 * Set the driver.
 * 
 * @param driver
 *            the driver.
 */
public void setDriver(String driver) {
    this.driver = driver;
}

/**
 * Get the responseTime.
 * @return the responseTime.
 */
public BrakeResponseTime getResponseTime() {
    return responseTime;
}

/**
 * Set the responseTime.
 * @param responseTime the responseTime.
 */
public void setResponseTime(BrakeResponseTime responseTime) {
    this.responseTime = responseTime;
}


}

1 个答案:

答案 0 :(得分:0)

我还没有意识到我可以通过这种方式访问​​bean容器的属性。

container.getIdByIndex(0).getCarMake()

这就是我要找的东西!