如何使用iTextpdf在pdf页面中显示整个列表?

时间:2019-05-07 04:56:44

标签: android arraylist itext android-sqlite

我在数据库中有两个表。通过将两者结合起来,我得到了一张新桌子。现在,我想在pdf页面中显示该表的值,但仅显示一行。如何在PdfTable中显示整个表的值。我在这里使用iText库。数据库方法在这里:

public List<PSummaryModel> testMethodSummary(){
    String ps = "Not Found";
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "select p_name, sum(i_amount) i_amount, sum(e_amount) e_amount, sum(i_amount)-sum(e_amount) profit " +
            "from (" +
            "select sr, date, project_id, expense_id expense_id, 0 i_amount, e_amount e_amount, description " +
            "from expense_voucher" +
            " union all " +
            "select sr, date, project_id, income_id income_id, i_amount i_amount, 0 e_amount, description " +
            "from income_voucher) v, projects p " +
            "where v.project_id=p.p_id " +
            "group by p_name ";
    Cursor cursor = db.rawQuery(query, null);
    List<PSummaryModel> summaryModelList = new ArrayList<>();
    while (cursor.moveToNext()){
        PSummaryModel pSummaryModel = new PSummaryModel(cursor.getString(0), cursor.getLong(1), cursor.getLong(2), cursor.getLong(3));
        summaryModelList.add(pSummaryModel);
    }
    return summaryModelList;
}

这是pdf表代码:

listOfSummaryModel = databaseHelper.testMethodSummary();
            for (PSummaryModel pSummaryModel : listOfSummaryModel) {

                PdfPCell cell = null;
                cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                cell = new PdfPCell(Phrase.getInstance(String.valueOf(pSummaryModel.getTotalIncome())));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                cell = new PdfPCell(Phrase.getInstance(String.valueOf(pSummaryModel.getTotalExpense())));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                cell = new PdfPCell(Phrase.getInstance(String.valueOf(pSummaryModel.getProfit())));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                document.add(tables);
                document.close();
            }

1 个答案:

答案 0 :(得分:0)

您添加表并在for循环的末尾关闭文档。即您仅在添加第一行之后执行此操作。

要解决此问题,请将这些指令移出循环,即更改此

for (PSummaryModel pSummaryModel : listOfSummaryModel) {

    PdfPCell cell = null;
    cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
    ...
    tables.addCell(cell);

    document.add(tables);
    document.close();
}

对此:

for (PSummaryModel pSummaryModel : listOfSummaryModel) {

    PdfPCell cell = null;
    cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
    ...
    tables.addCell(cell);
}

document.add(tables);
document.close();