android - Populate the ListView of a CursorAdapter with running totals -


i have listview shows list of transactions bank account, recent @ top down oldest @ bottom. have each listview item display snapshot of account balance after specific transaction, way down footerview show starting balance account. example, suppose open account $300, , make 2 $50 withdrawals , $10 deposit. see this:

| transdate | transamount | accountbalance | +-----------+-------------+----------------+ | 04/09/15  |    +$10     |     $210       | | 04/05/15  |    -$50     |     $200       | | 04/03/15  |    -$50     |     $250       | | 04/01/15  |    start    |     $300       | 

currently, transactions table holds transaction date, amount, , whether withdrawal or deposit, able display first 2 columns above.

i wrong, don't think it's safe hold account balance snapshot in database, field manipulated, , want best ensure counts reflective of current balance.

for same reason, i'd third column calculated value when listview populates, don't know how adjust cursoradapter or listview so. possible?

imho don't think it's bad idea keep balances in database. however, if gave task me, do:

  • create subclass of resourcecursoradapter. handle creating view list item.
  • give private sparsearray hold balances.
  • override swapcursor() iterate through cursor , generate balances each record. since each record has id, put balance in sparsearray id key. call super.swapcursor()
  • override bindview() date , amount cursor @ current position , put them in view. then, using record id cursor, balance sparsearray , put in view.

it's kind of brute force approach, not elegant. , performance degrade more transactions there are.

i think make work need reconciliation checkpoints don't start 0 balance @ beginning, rather start last reconciled balance. otherwise, when set cursor on adapter in turn calls swapcursor() method, it'll take forever generate balances before listview displays.


Popular posts from this blog