一點點的鹹菜在這裏。我正在閱讀Zip文件中的JSON,並且想要使用JSON的內容填充Vaadin中的表格。爪哇 - Vaadin - 表沒有填充
這裏是我的函數來讀取的東西,並填寫表,這是Java。
private void getJsonContent() {
try {
FileInputStream fis = new FileInputStream(backupFile);
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
byte[] buffer = new byte[1024];
while((entry = zin.getNextEntry()) != null) {
if(entry.getName().equalsIgnoreCase("content.json")) {
int n;
while((n = zin.read(buffer, 0, 1024)) > -1){
String JSON = new String(buffer, Charset.forName("UTF-8"));
JSONObject obj = new JSONObject(JSON);
logger.info(JSON);
// Assign "global" Values to Variables
this.createdAt = obj.getString("created_at");
this.version = obj.getString("version");
// Fill table if applicable
for(int i = 0; i < obj.getJSONArray("content").length(); i++) {
JSONObject sub = obj.getJSONArray("content").getJSONObject(i);
logger.info(sub);
infoTable.addItem(new Object[] {
sub.get("imported_identities").toString(),
sub.get("project_versions").toString(),
sub.get("last_import").toString(),
sub.get("client").toString(),
sub.get("project").toString()
}, i +1);
}
}
}
}
zin.close();
fis.close();
} catch (FileNotFoundException e) {
// Can't happen here
} catch (IOException e) {
logger.info("Can't read File.");
} catch (JSONException jse) {
logger.info("JSON Content could not be read: " + jse.getMessage());
}
}
你會發現我有一個函數調用logger.info(sub)
- 以確保我所得到的是另一種有效的JSON對象(我讀的文件包含嵌套的東西)
輸出:
{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client1","project":"Project2"}
{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client2","project":"Project1"}
{"imported_identities":0,"project_versions":1,"last_import":"2016-09-14T09:28:24.520Z","client":"Client1","project":"Project1"}
我確保所有的值都是正確的(並且表格默認爲null) - 這是表格屬性:
infoTable.addContainerProperty(impIds, String.class, null);
infoTable.addContainerProperty(projVe, String.class, null);
infoTable.addContainerProperty(lstImp, String.class, null);
infoTable.addContainerProperty(client, String.class, null);
infoTable.addContainerProperty(projct, String.class, null);
infoTable.setColumnCollapsingAllowed(true);
infoTable.setColumnCollapsed(impIds, true);
infoTable.setColumnCollapsed(projVe, true);
infoTable.setColumnCollapsed(lstImp, true);
最後,該表對其調用了「refreshRowCache」。任何人都看到了問題?有沒有錯誤的,什麼都沒有,表只是不添加的項目(的infoTable.getItemIds().size()
大小0後調用右邊是
編輯:
我嘗試以下,以驗證
infoTable.addItem(i + 1);
infoTable.getItem(i + 1).getItemProperty(impIds).setValue(sub.get("imported_identities").toString());
infoTable.getItem(i + 1).getItemProperty(projVe).setValue(sub.get("project_versions").toString());
。
這又引起一個NullPointerException異常,堆棧跟蹤但不包含任何我的課的,據我可以看到
作爲第一步,我將設置一個POJO類,併爲每個單元格連成員。使用包含JSON數據的POJO成功填充Java容器後,可以使用容器(例如BeanItemContainer)將其綁定到表。 'table.setContainerDataSource(new BeanItemContainer <>(POJO.class,yourListOfPojos))' –
Nevermind。過了太久。在我嘗試填充表格之前,我的表格未正確初始化,導致出現可預期的空白點。其餘的代碼工作。 – Eskir