Skip to content Skip to sidebar Skip to footer

Show Result From An Sqlite Database In Pdf Using Itext

I am a complete beginner in the coding World and I am trying to use IText to generate PDF from my App. I managed to create basic PDF with text, table etc.. Now I want to retrieve d

Solution 1:

Well actually the stack does tell you where the error is: "attempt to re-open an already-closed object".

When you do mCursor.close() basically you say the system "I won't use it anymore, you can free related resources".

IMHO you should have a class Expense, looking like:

publicclassExpense {
    privateint id, day, month, year;
    private String type;
    // and so on...publicintgetId() {
        return id;
    }

    publicvoidsetId(int id) {
        this.id = id;
    }
    // Other getters/setters

}

And your method getSepficItem() should returns a List:

public List<Expense> getSepficItem() {
    List<Expense> result = newArrayList();
    // Select All QuerySQLiteDatabasedb=this.getReadableDatabase();
    Stringq="SELECT * FROM Expense";
    CursormCursor= db.rawQuery(q, null);
    if (cursor.moveToFirst()) {
        do {
            Expenseexpense=newExpense();
            expense.setId(Integer.parseInt(cursor.getString(0)));
            // etc...
            expense.setType(cursor.getString(2));
            // etc...
            result.add(contact);
        } while (cursor.moveToNext());
    }
    mCursor.close();
    db.close();
    return result;
}

The the method that generates the PDF should use Expense, and not cursor.

Please note that it's not a full solution of your specific problem, rather a clue on how to organise your code.

Finally adapt the pdf creation:

createPdf() {

  for (Expense expense : db.getSepficItem()) {
    // ..table.addCell(expense.getId());
  }
}

Post a Comment for "Show Result From An Sqlite Database In Pdf Using Itext"