2014-01-10 60 views
-1

我想補充運行數據表中的項目我的類文件PrimeFaces的DataTable實現

package org.springframework.webflow.samples.booking; 


import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.UUID; 
import java.lang.String; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 

@ManagedBean 
@RequestScoped 

public class Car implements Serializable { 
    private final static String[] colors; 

    private final static String[] manufacturers; 

    private List<CarTemp> cars; 

    static { 
     colors = new String[10]; 
     colors[0] = "Black"; 
     colors[1] = "White"; 
     colors[2] = "Green"; 
     colors[3] = "Red"; 
     colors[4] = "Blue"; 
     colors[5] = "Orange"; 
     colors[6] = "Silver"; 
     colors[7] = "Yellow"; 
     colors[8] = "Brown"; 
     colors[9] = "Maroon"; 

     manufacturers = new String[10]; 
     manufacturers[0] = "Mercedes"; 
     manufacturers[1] = "BMW"; 
     manufacturers[2] = "Volvo"; 
     manufacturers[3] = "Audi"; 
     manufacturers[4] = "Renault"; 
     manufacturers[5] = "Opel"; 
     manufacturers[6] = "Volkswagen"; 
     manufacturers[7] = "Chrysler"; 
     manufacturers[8] = "Ferrari"; 
     manufacturers[9] = "Ford"; 
    } 


    public Car() { 
     cars = new ArrayList<CarTemp>(); 

     populateRandomCars(cars, 50); 
    } 

    private void populateRandomCars(List<Car> list, int size) { 

     CarTemp a= new CarTemp(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()); 
     for(int i = 0 ; i < size ; i++) 
      list.add(a); 

    } 

    public List<CarTemp> getCars() { 
     return cars; 
    } 

    private int getRandomYear() { 
     return (int) (Math.random() * 50 + 1960); 
    } 

    private String getRandomColor() { 
     return colors[(int) (Math.random() * 10)]; 
    } 

    private String getRandomManufacturer() { 
     return manufacturers[(int) (Math.random() * 10)]; 
    } 

    private String getRandomModel() { 
     return UUID.randomUUID().toString().substring(0, 8); 
    } 
} 

這是我的CarTemp.Java文件

package org.springframework.webflow.samples.booking; 
public class CarTemp { 

    private String model; 
    private int year; 
    private String manufacturer; 
    private String color; 

    public CarTemp(String model, int year, String manufacturer, String color) { 
     this.model = model; 
     this.year = year; 
     this.manufacturer = manufacturer; 
     this.color = color; 
    } 

    public String getModel() { 
     return model; 
    } 

    public void setModel(String model) { 
     this.model = model; 
    } 

    public int getYear() { 
     return year; 
    } 

    public void setYear(int year) { 
     this.year = year; 
    } 

    public String getManufacturer() { 
     return manufacturer; 
    } 

    public void setManufacturer(String manufacturer) { 
     this.manufacturer = manufacturer; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 
} 

我得到的錯誤:

java:方法org.springframework.webflow.samples.booking.Car中的populateRandomCars不能應用於給定的類型; 需要:java.util.List中,整數 發現:java.util.List中,整數 原因:實際參數的java.util.List不能通過方法調用轉換被轉換成的java.util.List

java的:無找到適合的方法add(org.springframework.webflow.samples.booking.CarTemp) 方法java.util.List.add(int,org.springframework.webflow.samples.booking.Car)不適用 (實際和正式參數列表的長度不同) 方法java.util.List.add(org.springframework.webflow.samples.booking.Car)不適用 (實際參數org.springframework.webflow.samples.booking.CarTemp無法轉換爲org.springframework.webflow.samples.booking.Car by m方法調用轉換)

它看起來像類可以添加到列表中。 在primefaces數據表文檔中,他們沒有關於系統如何工作的更多解釋。 我不知道我做了什麼錯誤。

回答

1

的問題是什麼populateRandomCars期待作爲參數,和你所傳遞給它

.... 

public Car() { 
    cars = new ArrayList<CarTemp>(); 
    populateRandomCars(cars, 50); // (2) 
} 

private void populateRandomCars(List<Car> list, int size) { // (1) 
... 
} 

.... 

(1)第一個參數是期待型轎車的列表 - >List<Car>
(2 )而你傳遞一個類型CarTemp的列表 - >List<CarTemp>

所以,你必須修改populateRandomCars這個簽名

private void populateRandomCars(List<CarTemp> list, int size) {}