如何填充GWT DataGrid?

时间:2014-09-03 09:37:14

标签: java gwt datagrid

我已经创建了一个DataGrid:

DataGrid<String> grid = new DataGrid<String>();
                grid.setPageSize(4);

                TextColumn<String> date = new TextColumn<String>() {
                    public String getValue(String object) {
                        return object;
                    }
                };
                grid.addColumn(date, "Date");

                TextColumn<String> time = new TextColumn<String>() {
                    public String getValue(String object) {
                        return object;
                    }
                };
                grid.addColumn(time, "Time");

                TextColumn<String> number = new TextColumn<String>() {
                    public String getValue(String object) {
                        return object;
                    }
                };
                grid.addColumn(number, "Number");

现在我想填充它,但我不知道该怎么做,因为我有这些String[]并且它们也必须分开:

textString[1]="01/01/2014;10:00;300";
textString[2]="02/02/2014;11:00;400"; ...

提前致谢。

更新 @Sturmination我按照你的提示,但这个代码还有另一个问题(假设分裂完成):

..
protected static List<Example> EXAMPLES = null;
..

public void method() {
...
EXAMPLES = Arrays.asList(new Example("01/01/2014", "10:00", "300"));
grid.setRowCount(EXAMPLES.size(), true);
grid.setRowData(0, EXAMPLES);
..
}

它返回此错误:

The method setRowData(int, List<? extends String>) in the type AbstractHasData<String> is not applicable for the arguments (int, List<Example>)

它表明:Change type of EXAMPLES to List<? extends String>但它无论如何都不起作用。

3 个答案:

答案 0 :(得分:2)

你试过这个:

DataGrid<String> grid = new DataGrid<String>();
grid.setPageSize(4);

TextColumn<String> date = new TextColumn<String>() {
    public String getValue(String object) {
        return object.split(';')[0];
    }
};
grid.addColumn(date, "Date");

TextColumn<String> time = new TextColumn<String>() {
    public String getValue(String object) {
        return object.split(';')[1];
    }
};
grid.addColumn(time, "Time");

TextColumn<String> number = new TextColumn<String>() {
    public String getValue(String object) {
        return object.split(';')[2];
    }
};
grid.addColumn(number, "Number");

更好的方法是为您的行创建一个新的Java Bean,并将String[]项转换为新行对象的List<Row>

答案 1 :(得分:1)

您可以在代码中使用String[]

DataGrid<String> grid = new DataGrid<String>();

    TextColumn<String[]> date = new TextColumn<String[]>() {
        public String getValue(String[] object) {
           return object[0];
        }
    };

添加字符串时拆分字符串:

private ListDataProvider<String[]> dataProvider = new ListDataProvider<String[]>();
private List<String[]> displayItems = dataProvider.getList();
...

for (String textString : textStrings) {
    displayItems.add(textString.split(";");
}

答案 2 :(得分:1)

如果您的数据规模不大,我会这样做。

创建课程

Class YourClass{    
  private String date, time, number;
  public YourClass(String date, String time, String nuber){
    this.date=date;
    this.time=time;
    this.number=number;
  }
  //getters and setters here
}

创建一个方法来汇总您的值

private List<YourObject> convert(String[] values){
  List<YourClass> data= new ArrayList<YourClass>();
  String date, time, number; 
  for(i=0; values.size(); i++){

    values[i] //split the string and asign values to date, time and number
    dataList.add(new YourClass(date, time, number));
  }
  return dataList;
}

将值添加到DataGrid

ListDataProvider<YourClass> provider= new ListDataProvider<YourClass>();
List<YourClass> dataList = new ArrayList<YourClass>();
dataList.addAll(convert(textString[]));
provider.addDataDisplay(grid);
provider.setList(dataList);

然后在你的专栏中:

return object.getDate();
return object.getTime();
return object.getNumber();