2323import android .content .DialogInterface ;
2424import android .content .Intent ;
2525import android .database .Cursor ;
26+ import android .os .AsyncTask ;
2627import android .os .Bundle ;
2728import android .support .v4 .app .DialogFragment ;
2829import android .support .v4 .app .Fragment ;
5556import org .gnucash .android .ui .widget .WidgetConfigurationActivity ;
5657import org .gnucash .android .util .OnAccountClickedListener ;
5758
59+ import java .lang .ref .WeakReference ;
5860import java .util .Locale ;
5961
6062/**
@@ -589,9 +591,6 @@ public void bindView(View v, Context context, Cursor cursor) {
589591 // perform the default binding
590592 super .bindView (v , context , cursor );
591593
592- // add a summary of transactions to the account view
593- TextView summary = (TextView ) v
594- .findViewById (R .id .transactions_summary );
595594 final long accountId = cursor .getLong (DatabaseAdapter .COLUMN_ROW_ID );
596595
597596 TextView subAccountTextView = (TextView ) v .findViewById (R .id .secondary_text );
@@ -603,11 +602,10 @@ public void bindView(View v, Context context, Cursor cursor) {
603602 } else
604603 subAccountTextView .setVisibility (View .GONE );
605604
606- Money balance = mAccountsDbAdapter .getAccountBalance (accountId );
607- summary .setText (balance .formattedString (Locale .getDefault ()));
608- int fontColor = balance .isNegative () ? getResources ().getColor (R .color .debit_red ) :
609- getResources ().getColor (R .color .credit_green );
610- summary .setTextColor (fontColor );
605+ // add a summary of transactions to the account view
606+ TextView summary = (TextView ) v
607+ .findViewById (R .id .transactions_summary );
608+ new AccountBalanceTask (summary , getActivity ()).execute (accountId );
611609
612610 ImageView newTransactionButton = (ImageView ) v .findViewById (R .id .btn_new_transaction );
613611 if (inSubAcccount ()){
@@ -628,4 +626,46 @@ public void onClick(View v) {
628626 }
629627 }
630628
629+ /**
630+ * An asynchronous task for computing the account balance of an account.
631+ * This is done asynchronously because in cases of deeply nested accounts,
632+ * it can take some time and would block the UI thread otherwise.
633+ */
634+ public static class AccountBalanceTask extends AsyncTask <Long , Void , Money > {
635+ private final WeakReference <TextView > accountBalanceTextViewReference ;
636+ private final AccountsDbAdapter accountsDbAdapter ;
637+
638+ public AccountBalanceTask (TextView balanceTextView , Context context ){
639+ accountBalanceTextViewReference = new WeakReference <TextView >(balanceTextView );
640+ accountsDbAdapter = new AccountsDbAdapter (context );
641+ }
642+
643+ @ Override
644+ protected Money doInBackground (Long ... params ) {
645+ //if the view for which we are doing this job is dead, kill the job as well
646+ if (accountBalanceTextViewReference == null || accountBalanceTextViewReference .get () == null ){
647+ cancel (true );
648+ accountsDbAdapter .close ();
649+ return Money .getZeroInstance ();
650+ }
651+ Money balance = accountsDbAdapter .getAccountBalance (params [0 ]);
652+ accountsDbAdapter .close ();
653+ return balance ;
654+ }
655+
656+ @ Override
657+ protected void onPostExecute (Money balance ) {
658+ if (accountBalanceTextViewReference != null && balance != null ){
659+ final Context context = accountsDbAdapter .getContext ();
660+ final TextView balanceTextView = accountBalanceTextViewReference .get ();
661+ if (balanceTextView != null ){
662+ balanceTextView .setText (balance .formattedString ());
663+ int fontColor = balance .isNegative () ? context .getResources ().getColor (R .color .debit_red ) :
664+ context .getResources ().getColor (R .color .credit_green );
665+ balanceTextView .setTextColor (fontColor );
666+ }
667+ }
668+ }
669+ }
670+
631671}
0 commit comments