gt:         if (categoryId != 0) {
context: /*******************************************************************************
   * Copyright (c) 2010 Denis Solonenko.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the GNU Public License v2.0
   * which accompanies this distribution, and is available at
   * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
   *
   * Contributors:
   *     Denis Solonenko - initial API and implementation
   *     Emmanuel Florent - port to Android API 11+
   ******************************************************************************/
  package com.flowzr.adapter;
  
  import android.content.Context;
  import android.content.res.Resources;
  import android.database.Cursor;
  import android.graphics.Color;
  import android.graphics.drawable.Drawable;
  import android.text.format.DateUtils;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.CheckBox;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.RelativeLayout;
  import android.widget.ResourceCursorAdapter;
  import android.widget.TextView;
  
  import com.flowzr.R;
  import com.flowzr.db.DatabaseAdapter;
  import com.flowzr.db.DatabaseHelper.BlotterColumns;
  import com.flowzr.db.MyEntityManager;
  import com.flowzr.model.CategoryEntity;
  import com.flowzr.model.Currency;
  import com.flowzr.model.TransactionStatus;
  import com.flowzr.recur.Recurrence;
  import com.flowzr.utils.CurrencyCache;
  import com.flowzr.utils.MyPreferences;
  import com.flowzr.utils.Utils;
  
  import java.util.Calendar;
  import java.util.Date;
  import java.util.HashMap;
  
  import static com.flowzr.model.Category.isSplit;
  import static com.flowzr.utils.TransactionTitleUtils.generateTransactionTitle;
  
  public class BlotterListAdapter extends ResourceCursorAdapter {
  
      protected final Date dt = new Date();
  
      protected final StringBuilder sb = new StringBuilder();
      protected final Drawable icBlotterIncome;
      protected final Drawable icBlotterExpense;
      protected final Drawable icBlotterTransfer;
      protected final Drawable icBlotterSplit;
      protected final Utils u;
      protected final DatabaseAdapter db;
      protected final MyEntityManager em;
  
      private final int colors[];
  
      private boolean allChecked = true;
      private final HashMap<Long, Boolean> checkedItems = new HashMap<>();
  
      private boolean showRunningBalance;
  
      public BlotterListAdapter(Context context, DatabaseAdapter db, Cursor c) {
          this(context, db, R.layout.blotter_list_item, c, false);
      }
  
      public BlotterListAdapter(Context context, DatabaseAdapter db, int layoutId, Cursor c) {
          this(context, db, layoutId, c, false);
      }
  
      public BlotterListAdapter(Context context, DatabaseAdapter db, int layoutId, Cursor c, boolean autoRequery) {
          super(context, layoutId, c, autoRequery);
          this.icBlotterIncome = context.getResources().getDrawable(R.drawable.ic_blotter_income);
          this.icBlotterExpense = context.getResources().getDrawable(R.drawable.ic_blotter_expense);
          this.icBlotterTransfer = context.getResources().getDrawable(R.drawable.ic_blotter_transfer);
          this.icBlotterSplit = context.getResources().getDrawable(R.drawable.ic_action_share);
          this.u = new Utils(context);
          this.colors = initializeColors(context);
          this.showRunningBalance = MyPreferences.isShowRunningBalance(context);
          //int topPadding = context.getResources().getDimensionPixelSize(R.dimen.transaction_icon_padding);
          this.db = db;
          this.em = db.em();
      }
  
      private int[] initializeColors(Context context) {
          Resources r = context.getResources();
          TransactionStatus[] statuses = TransactionStatus.values();
          int count = statuses.length;
          int[] colors = new int[count];
          for (int i = 0; i < count; i++) {
              colors[i] = r.getColor(statuses[i].colorId);
          }
          return colors;
      }
  
      protected boolean isShowRunningBalance() {
          return showRunningBalance;
      }
  
      @Override
      public View newView(Context context, Cursor cursor, ViewGroup parent) {
          View view = super.newView(context, cursor, parent);
          createHolder(view);
          return view;
      }
  
      protected void createHolder(View view) {
          BlotterViewHolder h = new BlotterViewHolder(view);
          view.setTag(h);
      }
  
      @Override
      public void bindView(View view, Context context, Cursor cursor) {
          final BlotterViewHolder v = (BlotterViewHolder) view.getTag();
          bindView(v, context, cursor);
      }
  
      protected void bindView(final BlotterViewHolder v, Context context, Cursor cursor) {
          long toAccountId = cursor.getLong(BlotterColumns.to_account_id.ordinal());
          int isTemplate = cursor.getInt(BlotterColumns.is_template.ordinal());
          TextView noteView = isTemplate == 1 ? v.bottomView : v.centerView;
          
          if (toAccountId > 0) {
              v.topView.setText(R.string.transfer);
  
              String fromAccountTitle = cursor.getString(BlotterColumns.from_account_title.ordinal());
              String toAccountTitle = cursor.getString(BlotterColumns.to_account_title.ordinal());
              u.setTransferTitleText(noteView, fromAccountTitle, toAccountTitle);
  
              long fromCurrencyId = cursor.getLong(BlotterColumns.from_account_currency_id.ordinal());
              Currency fromCurrency = CurrencyCache.getCurrency(em, fromCurrencyId);
              long toCurrencyId = cursor.getLong(BlotterColumns.to_account_currency_id.ordinal());
              Currency toCurrency = CurrencyCache.getCurrency(em, toCurrencyId);
  
              int dateViewColor = v.bottomView.getCurrentTextColor();
  
              long fromAmount = cursor.getLong(BlotterColumns.from_amount.ordinal());
              long toAmount = cursor.getLong(BlotterColumns.to_amount.ordinal());
              long fromBalance = cursor.getLong(BlotterColumns.from_account_balance.ordinal());
              long toBalance = cursor.getLong(BlotterColumns.to_account_balance.ordinal());
              u.setTransferAmountText(v.rightView, fromCurrency, fromAmount, toCurrency, toAmount);
              v.rightView.setTextColor(dateViewColor);
              if (v.rightCenterView != null) {
                  u.setTransferBalanceText(v.rightCenterView, fromCurrency, fromBalance, toCurrency, toBalance);
              }
              v.iconView.setImageDrawable(icBlotterTransfer);
          } else {
              String fromAccountTitle = cursor.getString(BlotterColumns.from_account_title.ordinal());
              v.topView.setText(fromAccountTitle);
              setTransactionTitleText(cursor, noteView);
              sb.setLength(0);
              long fromCurrencyId = cursor.getLong(BlotterColumns.from_account_currency_id.ordinal());
              Currency fromCurrency = CurrencyCache.getCurrency(em, fromCurrencyId);
              long amount = cursor.getLong(BlotterColumns.from_amount.ordinal());
              long originalCurrencyId = cursor.getLong(BlotterColumns.original_currency_id.ordinal());
              if (originalCurrencyId > 0) {
                  Currency originalCurrency = CurrencyCache.getCurrency(em, originalCurrencyId);
                  long originalAmount = cursor.getLong(BlotterColumns.original_from_amount.ordinal());
                  u.setAmountText(sb, v.rightView, originalCurrency, originalAmount, fromCurrency, amount, true);
              } else {
                  u.setAmountText(sb, v.rightView, fromCurrency, amount, true);
              }
              long categoryId = cursor.getLong(BlotterColumns.category_id.ordinal());
              if (isSplit(categoryId)) {
                  v.iconView.setImageDrawable(icBlotterSplit);
              } else if (amount == 0) {
                  int categoryType = cursor.getInt(BlotterColumns.category_type.ordinal());
                  if (categoryType == CategoryEntity.TYPE_INCOME) {
                      v.iconView.setImageDrawable(icBlotterIncome);
                  } else if (categoryType == CategoryEntity.TYPE_EXPENSE) {
                      v.iconView.setImageDrawable(icBlotterExpense);
                  }
              } else {
                  if (amount > 0) {
                      v.iconView.setImageDrawable(icBlotterIncome);
                  } else if (amount < 0) {
                      v.iconView.setImageDrawable(icBlotterExpense);
                  }
              }
              if (v.rightCenterView != null) {
                  long balance = cursor.getLong(BlotterColumns.from_account_balance.ordinal());
                  v.rightCenterView.setText(Utils.amountToString(fromCurrency, balance, false));
              }
          }
  
  
          if (isTemplate == 1) {
              String templateName = cursor.getString(BlotterColumns.template_name.ordinal());
              v.centerView.setText(templateName);
          } else {
              String recurrence = cursor.getString(BlotterColumns.recurrence.ordinal());
              if (isTemplate == 2 && recurrence != null) {
                  Recurrence r = Recurrence.parse(recurrence);
                  v.bottomView.setText(r.toInfoString(context));
                  v.bottomView.setTextColor(v.topView.getTextColors().getDefaultColor());
              } else {
                  setIndicatorColor(v, cursor);
                  long date = cursor.getLong(BlotterColumns.datetime.ordinal());
                  dt.setTime(date);
                  v.bottomView.setText(DateUtils.formatDateTime(context, dt.getTime(),
                          DateUtils.FORMAT_SHOW_TIME));
  
                  v.month_TV.setText(DateUtils.formatDateTime(context, dt.getTime(),
                  		DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT | DateUtils.FORMAT_ABBREV_MONTH));
  
                  Calendar cal=Calendar.getInstance();
                  cal.setTimeInMillis(date);                
                  v.day_TV.setText(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
                                
                  if (isTemplate == 0 && date > System.currentTimeMillis()) {
                      u.setFutureTextColor(v.bottomView);
                      u.setFutureTextColor(v.day_TV);                    
                      u.setFutureTextColor(v.month_TV);                    
                  } else {
                      v.bottomView.setTextColor(v.topView.getTextColors().getDefaultColor());
                  }
              }
          }
          removeRightCenterViewIfNeeded(v);
          if (v.checkBox != null) {
              final long id = cursor.getLong(BlotterColumns._id.ordinal());
              v.checkBox.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View arg0) {
                      updateCheckedState(id, allChecked ^ v.checkBox.isChecked());
                  }
              });
              boolean isChecked = getCheckedState(id);
              v.checkBox.setChecked(isChecked);
          }
      }
  
      private void setTransactionTitleText(Cursor cursor, TextView noteView) {
          sb.setLength(0);
          String payee = cursor.getString(BlotterColumns.payee.ordinal());
          String note = cursor.getString(BlotterColumns.note.ordinal());
          long locationId = cursor.getLong(BlotterColumns.location_id.ordinal());
          String location = getLocationTitle(cursor, locationId);
          long categoryId = cursor.getLong(BlotterColumns.category_id.ordinal());
          String category = getCategoryTitle(cursor, categoryId);
          String text = generateTransactionTitle(sb, payee, note, location, categoryId, category);
          noteView.setText(text);
          noteView.setTextColor(Color.WHITE);
      }
  
      private String getCategoryTitle(Cursor cursor, long categoryId) {
          String category = "";
  