Java tutorial
/* * 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.database.Cursor; import android.os.Bundle; import android.support.v4.content.Loader; import android.view.View; import android.widget.TextView; import java.util.Calendar; import de.aw.awlib.database.AbstractDBHelper; import de.aw.awlib.views.AWTextCurrency; import de.aw.monma.R; import de.aw.monma.database.DBConvert; import de.aw.monma.database.DBDefinition; import de.aw.monma.gv.Account; import de.aw.monma.gv.Account.AccountNotFoundException; /** * Uebersicht ueber Depotbestaende und Ertraege des aktuellen Jahres. */ public class SnippetDepotUebersicht extends SnippetTemplate { private static final int detailView = R.layout.snippet_depots_details; private static final DBDefinition tbd = DBDefinition.WPBestand; private static final String selection = "NOT " + column_ausgeblendet; private static final DBDefinition tbdUmsatz = DBDefinition.UmsatzView; private long einstand; /** * Auswahl der Ertraege (Einnahmen/Ausgaben/Gewinne/Verluste). Abfrage auf Buchung und Umsatzart * = 0 (WPUmsatz) */ private String[] ertragProjection = new String[] { AbstractDBHelper.SQLSumItem(column_amount) }; private String ertragSelection = column_accountID + " = ? AND " + column_btag + "> ?"; private long gesamtertrag; private String groupBy = column_accountID; private int indexAccountID; private int indexEinstand; private int indexMarkwert; private int layoutHeader = R.layout.snippet_depots; private long marktwert; private String orderBy = column_accountID + " DESC"; private String[] projection = new String[] { column_accountID, _id, AbstractDBHelper.SQLSumItem(column_bruttoumsatz), AbstractDBHelper.SQLSumItem(column_marktwert) }; private int[] viewResIDs = new int[] { R.id.tvKontoname, R.id.tvEinstandsPreis, R.id.tvWPMarktwert, R.id.tvBuchgewinn, R.id.tvErtraege, R.id.tvPercent }; private Integer year; @Override protected int headerView() { return layoutHeader; } @Override protected int noDataText() { return R.string.snippetKeineDepots; } @Override protected void onBindView(View view, int resID, Cursor cursor, int cursorPosition) { TextView tv; AWTextCurrency tvc; switch (resID) { case R.id.tvBuchgewinn: tvc = (AWTextCurrency) view; long value = marktwert - einstand; tvc.setValue(value); break; case R.id.tvErtraege: tvc = (AWTextCurrency) view; Long accountID = cursor.getLong(indexAccountID); if (accountID != 0) { String[] ertragSelectionArgs = new String[] { accountID.toString(), year.toString() + "-01-01" }; Cursor c = getActivity().getContentResolver().query(tbdUmsatz.getUri(), ertragProjection, ertragSelection, ertragSelectionArgs, null); if (c.moveToFirst()) { long ertrag = c.getLong(0); gesamtertrag += ertrag; tvc.setValue(ertrag); } c.close(); } else { tvc.setValue(gesamtertrag); } break; case R.id.tvKontoname: tv = (TextView) view; try { String accountName = Account.getAccountName(cursor.getInt(cursorPosition)); tv.setText(accountName); } catch (AccountNotFoundException e) { tv.setText(R.string.tvGesamt); } break; case R.id.tvEinstandsPreis: einstand = cursor.getLong(indexEinstand); tvc = (AWTextCurrency) view; tvc.setValue(einstand); break; case R.id.tvWPMarktwert: marktwert = cursor.getLong(indexMarkwert); tvc = (AWTextCurrency) view; tvc.setValue(marktwert); break; case R.id.tvPercent: long percent = ((marktwert - einstand) * 10000) / einstand; tv = (TextView) view; tv.setText(DBConvert.convertPercent(percent)); break; } } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { gesamtertrag = 0L; indexAccountID = cursor.getColumnIndexOrThrow(column_accountID); indexEinstand = cursor.getColumnIndexOrThrow(column_bruttoumsatz); indexMarkwert = cursor.getColumnIndexOrThrow(column_marktwert); super.onLoadFinished(loader, cursor); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); TextView tv = view.findViewById(R.id.tvErtragYear); String text = getString(R.string.ertraege) + " " + year; tv.setText(text); } @Override protected void setInternalArguments(Bundle args) { super.setInternalArguments(args); args.putParcelable(DBDEFINITION, tbd); args.putInt(VIEWHOLDERLAYOUT, detailView); args.putIntArray(VIEWRESIDS, viewResIDs); args.putStringArray(PROJECTION, projection); args.putString(SELECTION, selection); args.putString(GROUPBY, groupBy); args.putString(ORDERBY, orderBy); Calendar cal = Calendar.getInstance(); year = cal.get(Calendar.YEAR); } }