@Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    final MyExpenses ctx = (MyExpenses) getActivity();
    Context wrappedCtx = DialogUtils.wrapContext2(ctx);
    if (mTransaction == null) {
      return new AlertDialog.Builder(wrappedCtx)
          .setMessage("Transaction has been deleted")
          .create();
    }
    final LayoutInflater li = LayoutInflater.from(wrappedCtx);
    View view = li.inflate(R.layout.transaction_detail, null);
    int title;
    boolean type =
        mTransaction.amount.getAmountMinor() > 0 ? ExpenseEdit.INCOME : ExpenseEdit.EXPENSE;
    if (mTransaction instanceof SplitTransaction) {
      // TODO: refactor duplicated code with SplitPartList
      title = R.string.split_transaction;
      View emptyView = view.findViewById(R.id.empty);
      Resources.Theme theme = ctx.getTheme();
      TypedValue color = new TypedValue();
      theme.resolveAttribute(R.attr.colorExpense, color, true);
      final int colorExpense = color.data;
      theme.resolveAttribute(R.attr.colorIncome, color, true);
      final int colorIncome = color.data;

      ListView lv = (ListView) view.findViewById(R.id.list);
      // Create an array to specify the fields we want to display in the list
      String[] from = new String[] {KEY_LABEL_MAIN, KEY_AMOUNT};

      // and an array of the fields we want to bind those fields to
      int[] to = new int[] {R.id.category, R.id.amount};

      final String categorySeparator, commentSeparator;
      categorySeparator = " : ";
      commentSeparator = " / ";
      // Now create a simple cursor adapter and set it to display
      mAdapter =
          new SimpleCursorAdapter(ctx, R.layout.split_part_row, null, from, to, 0) {
            /* (non-Javadoc)
             * calls {@link #convText for formatting the values retrieved from the cursor}
             * @see android.widget.SimpleCursorAdapter#setViewText(android.widget.TextView, java.lang.String)
             */
            @Override
            public void setViewText(TextView v, String text) {
              switch (v.getId()) {
                case R.id.amount:
                  text = Utils.convAmount(text, mTransaction.amount.getCurrency());
              }
              super.setViewText(v, text);
            }
            /* (non-Javadoc)
             * manipulates the view for amount (setting expenses to red) and
             * category (indicate transfer direction with => or <=
             * @see android.widget.CursorAdapter#getView(int, android.view.View, android.view.ViewGroup)
             */
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
              View row = super.getView(position, convertView, parent);
              TextView tv1 = (TextView) row.findViewById(R.id.amount);
              Cursor c = getCursor();
              c.moveToPosition(position);
              int col = c.getColumnIndex(KEY_AMOUNT);
              long amount = c.getLong(col);
              if (amount < 0) {
                tv1.setTextColor(colorExpense);
                // Set the background color of the text.
              } else {
                tv1.setTextColor(colorIncome);
              }
              TextView tv2 = (TextView) row.findViewById(R.id.category);
              if (Build.VERSION.SDK_INT < 11) tv2.setTextColor(Color.WHITE);
              String catText = tv2.getText().toString();
              if (DbUtils.getLongOrNull(c, KEY_TRANSFER_PEER) != null) {
                catText = ((amount < 0) ? "=&gt; " : "&lt;= ") + catText;
              } else {
                Long catId = DbUtils.getLongOrNull(c, KEY_CATID);
                if (catId == null) {
                  catText = getString(R.string.no_category_assigned);
                } else {
                  col = c.getColumnIndex(KEY_LABEL_SUB);
                  String label_sub = c.getString(col);
                  if (label_sub != null && label_sub.length() > 0) {
                    catText += categorySeparator + label_sub;
                  }
                }
              }
              col = c.getColumnIndex(KEY_COMMENT);
              String comment = c.getString(col);
              if (comment != null && comment.length() > 0) {
                catText += (catText.equals("") ? "" : commentSeparator) + "<i>" + comment + "</i>";
              }
              tv2.setText(Html.fromHtml(catText));
              return row;
            }
          };
      lv.setAdapter(mAdapter);
      lv.setEmptyView(emptyView);
      LoaderManager manager = ctx.getSupportLoaderManager();
      if (manager.getLoader(MyExpenses.SPLIT_PART_CURSOR) != null
          && !manager.getLoader(MyExpenses.SPLIT_PART_CURSOR).isReset())
        manager.restartLoader(MyExpenses.SPLIT_PART_CURSOR, null, this);
      else manager.initLoader(MyExpenses.SPLIT_PART_CURSOR, null, this);
    } else {
      view.findViewById(R.id.SplitContainer).setVisibility(View.GONE);
      if (mTransaction instanceof Transfer) {
        title = R.string.transfer;
        ((TextView) view.findViewById(R.id.AccountLabel)).setText(R.string.transfer_from_account);
        ((TextView) view.findViewById(R.id.CategoryLabel)).setText(R.string.transfer_to_account);
      } else title = type ? R.string.income : R.string.expense;
    }
    String accountLabel = Account.getInstanceFromDb(mTransaction.accountId).label;
    if (mTransaction instanceof Transfer) {
      ((TextView) view.findViewById(R.id.Account))
          .setText(type ? mTransaction.label : accountLabel);
      ((TextView) view.findViewById(R.id.Category))
          .setText(type ? accountLabel : mTransaction.label);
    } else {
      ((TextView) view.findViewById(R.id.Account)).setText(accountLabel);
      if ((mTransaction.catId != null && mTransaction.catId > 0)) {
        ((TextView) view.findViewById(R.id.Category)).setText(mTransaction.label);
      } else {
        view.findViewById(R.id.CategoryRow).setVisibility(View.GONE);
      }
    }
    ((TextView) view.findViewById(R.id.Date))
        .setText(
            DateFormat.getDateInstance(DateFormat.FULL).format(mTransaction.getDate())
                + " "
                + DateFormat.getTimeInstance(DateFormat.SHORT).format(mTransaction.getDate()));
    ((TextView) view.findViewById(R.id.Amount))
        .setText(
            Utils.formatCurrency(
                new Money(
                    mTransaction.amount.getCurrency(),
                    Math.abs(mTransaction.amount.getAmountMinor()))));
    if (!mTransaction.comment.equals(""))
      ((TextView) view.findViewById(R.id.Comment)).setText(mTransaction.comment);
    else view.findViewById(R.id.CommentRow).setVisibility(View.GONE);
    if (!mTransaction.referenceNumber.equals(""))
      ((TextView) view.findViewById(R.id.Number)).setText(mTransaction.referenceNumber);
    else view.findViewById(R.id.NumberRow).setVisibility(View.GONE);
    if (!mTransaction.payee.equals(""))
      ((TextView) view.findViewById(R.id.Payee)).setText(mTransaction.payee);
    else view.findViewById(R.id.PayeeRow).setVisibility(View.GONE);
    if (mTransaction.methodId != null)
      ((TextView) view.findViewById(R.id.Method))
          .setText(PaymentMethod.getInstanceFromDb(mTransaction.methodId).getDisplayLabel());
    else view.findViewById(R.id.MethodRow).setVisibility(View.GONE);
    if (Account.getInstanceFromDb(mTransaction.accountId).type.equals(Type.CASH))
      view.findViewById(R.id.StatusRow).setVisibility(View.GONE);
    else {
      TextView tv = (TextView) view.findViewById(R.id.Status);
      tv.setBackgroundColor(mTransaction.crStatus.color);
      tv.setText(mTransaction.crStatus.toString());
    }
    return new AlertDialog.Builder(getActivity())
        .setTitle(title)
        .setView(view)
        .setNegativeButton(android.R.string.ok, this)
        .setPositiveButton(R.string.menu_edit, this)
        .create();
  }