jeudi 26 mars 2015

How to use serialized data as parameter for sqlite query

So this is the general flow of my app:


Main Menu -> Account Manager -> Account Details -> Payment Schedule


The data from the user's selection in Account Manager is passed to Account Details through a Serialized Object. Then from Account Details the same Serialized Object is then passed to Payment Schedule for calculation purposes and all the results are thrown into my database specifically into PayTable.


Each account can have multiple payments so to make sure I can associate the proper payment entries to the proper account I have a GroupID in my PayTable. So now entries into PayTable are assigned a GroupID which is based on the AccountID passed to Payment Schedule.


So if AccountID = 1 then GroupID = 1.


Using this I can filter out the proper payments for the proper account and then display them in my GridView in Payments Schedule.


The problem


I can display the specific payments accordingly but only if I manually filter it, meaning I have to specify the GroupID my query filters by. My problem now is making my query adapt to the AccountID from the Serialized Object passed to Payment Schedule which I can't seem to figure out how to do. I've tried a few things but to no avail so here I am asking for a bit of help.


I don't know if I'm just phrasing my question wrong which is why I can't seem to find a solution or if it's just really not possible. Any help would be greatly appreciated.


Below are the relevant codes. If anything else is needed just ask.


Payment Schedule's GridView:



public void Paygrid(){

dbHelper = new DatabaseHelper(this);

Cursor a = dbHelper.getAllPayments();
startManagingCursor(a);

String[] from = new String[]{DatabaseHelper.colPayBal, DatabaseHelper.colInterest, DatabaseHelper.colPayDue, DatabaseHelper.colDateDue, DatabaseHelper.colPaid};
int[] to = new int[]{R.id.Amount,R.id.Interest, R.id.Total, R.id.DateDue, R.id.Paid};

SimpleCursorAdapter saa = new SimpleCursorAdapter(this, R.layout.paygrid, a, from, to);
intgrid.setAdapter(saa);
}


DatabaseHelper's query getAllPayments(): The ? is where I specify the GroupID



Cursor getAllPayments() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = db.rawQuery("SELECT " + colPayID + " as _id, " + colGroupID +
"," + colPayBal +
"," + colInterest +
"," + colPayDue +
"," + colDateDue +
"," + colPaid +
" from " +
payTable +
" WHERE " +
colGroupID + "=?", new String[]{} );
return cur;
}

Aucun commentaire:

Enregistrer un commentaire