Itext 如何将动态单元格添加到此代码中?

Itext 如何将动态单元格添加到此代码中?,itext,Itext,我有这段代码可以用itext从mysqlite生成一个表,但是单元格的数量是静态的(只有2个)。不管我在数据库中有多少记录,你能帮我根据记录的数量获得动态单元格吗?非常感谢 // Table PdfPTable table = new PdfPTable(2); // Header PdfPCell cell1 = new PdfPCell(new Phrase("groups")); PdfPCell cell2 = new PdfPCell(new Phras

我有这段代码可以用itext从mysqlite生成一个表,但是单元格的数量是静态的(只有2个)。不管我在数据库中有多少记录,你能帮我根据记录的数量获得动态单元格吗?非常感谢

// Table
    PdfPTable table = new PdfPTable(2);
    // Header
    PdfPCell cell1 = new PdfPCell(new Phrase("groups"));
    PdfPCell cell2 = new PdfPCell(new Phrase("machines"));
    table.addCell(cell1);
    table.addCell(cell2);
    // Data
    db = new Database(FirstActivity.this);
    Cursor c = db.getGroupsNamesMachines();
    if (c.moveToFirst()) {
        do {
            String group = c.getString(c.getColumnIndex("groupname"));
            cell1 = new PdfPCell(new Phrase(group));
            table.addCell(cell1);
            String machine = c.getString(c.getColumnIndex("machinename"));
            cell2 = new PdfPCell(new Phrase(machine));
            table.addCell(cell2);
        } while (c.moveToNext());
    }
    document.add(table);

您将单元格与列混淆,字段与行混淆

字段的数量是预先知道的。如果有
“选择groupname,machinename FROM groupmachines;”
,您知道每条记录将有两个字段,因此需要两列。如果您有
“从groupmachines中选择groupid、groupname、machinename;”
,则需要3列,依此类推

记录的数量事先不知道。您执行一个查询,
光标
会一直为您提供数据,只要还有记录。这不是问题:只需为每条记录向表中添加一行即可。在iText中隐式地添加一行:只要添加了与列数量相同的单元格,就有了完整的行


一旦您将表格添加到文档中,如果表格不适合单个页面,那么这些行将分布在不同的页面上。

那么为什么它只显示前两组,而其他组不显示在表格上呢?您的说法是错误的。看看这个例子。数据库中有50个状态,因此数据库中有50个正文行。在你的循环中添加
System.out.println(“group”)
,你会发现你在
光标上循环的笨拙方式是罪魁祸首:你只看两行。如果你要求不高,你能修好我的代码吗?不,因为我从来没有使用过
光标
;我总是使用
ResultSet
。我不明白你为什么要使用
moveToFirst()
c.moveToNext()
。你是如何解决这个问题的?(Als在床上,我喝了几杯鸡尾酒,所以我现在不是很专业)