网格g =新网格(arg0.size(),5);
g.setBorderWidth(5);
g.setTitle("users data");
g.setVisible(true);
g.setCellSpacing(3);
g.setCellPadding(2);
g.setText(0,0, "empid");
g.setText(0,1, "first name");
g.setText(0,2, "last name");
g.setText(0,3, "gender");
g.setText(0,4, "studies");
System.err.println("list = "+arg0);
for(int i=0;i<arg0.size();i++)
{
User data= (User) arg0.get(i);
for(int row=1;row<arg0.size();row++)
{
for(int col=0;col<5;col++)
{
g.setText(row, col,String.valueOf(data.getEmpid()));
g.setText(row, col,data.getFirstname());
g.setText(row, col, data.getLastname());
g.setText(row, col,data.getGender());
g.setText(row, col, data.getStudies());
}
}
}
RootPanel.get().clear();
RootPanel.get().add(g);
arg0是用户pojo的列表。在每一列中,最后一个字段只是添加,但我们必须在每列中添加适当的数据。大部分我在for循环中做错了请纠正我并给予回复
答案 0 :(得分:0)
要正确添加数据,请使用如下的for循环:
// make the grid the size of the userlist + 1 for the header line
Grid g = new Grid(arg0.size() + 1, 5);
// rest of the Grid-definition
;
// per user in the user-list, add one row
for(int i=0;i<arg0.size();i++){
// retrieve the current user
User data= (User) arg0.get(i);
// add the data of that user in the correct column
// concerning the row: add in currentRow+1 to compensate for header row
g.setText(i+1, 0,String.valueOf(data.getEmpid()));
g.setText(i+1, 1,data.getFirstname());
g.setText(i+1, 2, data.getLastname());
g.setText(i+1, 3,data.getGender());
g.setText(i+1, 4, data.getStudies());
}
然而,另一种方法是使用CellTable。它比您的网格更灵活,您可以轻松添加/删除行,并且它更容易,例如如果将来某个时候需要更改列的顺序。此外,它还提供了必要的列排序接口。如果您对查看CellTable示例感兴趣,请与我们联系,以便我可以更新此帖子。