de.aw.monma.snippets.SnippetRegelmBuchungen.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.snippets.SnippetRegelmBuchungen.java

Source

/*
 * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen
 *
 * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany]
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if
 * not, see <http://www.gnu.org/licenses/>.
 */
package de.aw.monma.snippets;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;

import de.aw.awlib.views.AWTextCurrency;
import de.aw.monma.R;
import de.aw.monma.cash.ActivityCash;
import de.aw.monma.database.DBConvert;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.gv.Account;

/**
 * Snippet fuer Anzeige Regelmaessige Buchungen.
 */
public class SnippetRegelmBuchungen extends SnippetTemplate {
    private static final int detailView = R.layout.snippet_regelmbuchungen_details;
    private static final int layoutHeader = R.layout.snippet_regelmbuchungen;
    private static final DBDefinition tbd = DBDefinition.BankRegelmView;
    private static final String[] projection = new String[] { column_accountID, column_btag, column_partnername,
            column_amount, _id };
    private static final String selection = column_btag + " < ?" + " AND " + column_transferID + " IS NULL";
    private static final int[] viewResIDs = new int[] { R.id.tvKontoname, R.id.tvUmsatzDatum, R.id.tvEmpfaenger,
            R.id.tvBetrag };
    private String accountName;

    @Override
    protected int headerView() {
        return layoutHeader;
    }

    @Override
    protected int noDataText() {
        return R.string.snippetKeineZahlungserinnerungen;
    }

    @Override
    protected void onBindView(View view, int resID, Cursor cursor, int cursorPosition) {
        TextView tv;
        switch (resID) {
        case R.id.tvKontoname:
            int accountID = cursor.getInt(cursorPosition);
            String text = Account.getAccountName(accountID);
            if (text.equals(accountName)) {
                view.setVisibility(View.GONE);
            } else {
                view.setVisibility(View.VISIBLE);
                accountName = text;
                tv = (TextView) view;
                tv.setText(accountName);
            }
            break;
        case R.id.tvUmsatzDatum:
            tv = (TextView) view;
            tv.setText(DBConvert.convertDate(cursor.getString(cursorPosition)));
            break;
        case R.id.tvEmpfaenger:
            tv = (TextView) view;
            tv.setText(cursor.getString(cursorPosition));
            break;
        case R.id.tvBetrag:
            AWTextCurrency tvc = (AWTextCurrency) view;
            tvc.setValue(cursor.getLong(cursorPosition));
            break;
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), ActivityCash.class);
        intent.putExtra(ACTION, (Parcelable) Action.ShowRegelmBuchungen);
        startActivity(intent);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        // Das folgende muss explizit so gesetzt werden, sonst wird die View
        // nicht richtig gefuellt.
        accountName = null;
        super.onLoadFinished(loader, cursor);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putParcelable(DBDEFINITION, tbd);
        args.putIntArray(VIEWRESIDS, viewResIDs);
        args.putInt(VIEWHOLDERLAYOUT, detailView);
        args.putStringArray(PROJECTION, projection);
        args.putString(SELECTION, selection);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 5);
        Date date = cal.getTime();
        String[] selectionArgs = { DBConvert.convertDate2SQLiteDate(date) };
        args.putStringArray(SELECTIONARGS, selectionArgs);
    }
}