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:
public class Expense {
private int id, day, month, year;
private String type;
// and so on...
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// Other getters/setters
}
And your method getSepficItem() should returns a List:
public List<Expense> getSepficItem() {
List<Expense> result = new ArrayList();
// Select All Query
SQLiteDatabase db = this.getReadableDatabase();
String q = "SELECT * FROM Expense";
Cursor mCursor = db.rawQuery(q, null);
if (cursor.moveToFirst()) {
do {
Expense expense = new Expense();
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"