de.aw.monma.mainscreen.FragmentDepots.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.mainscreen.FragmentDepots.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.mainscreen;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.content.Loader;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import de.aw.awlib.adapters.AWItemListAdapter;
import de.aw.awlib.database.AbstractDBHelper;
import de.aw.awlib.recyclerview.AWItemListRecyclerViewFragment;
import de.aw.awlib.recyclerview.AWLibViewHolder;
import de.aw.monma.BR;
import de.aw.monma.R;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.database.TableColums;
import de.aw.monma.gv.WertpapierBestand;
import de.aw.monma.monmamain.MonMaInterface;
import de.aw.monma.wertpapier.ActivityWertpapiere;

import static de.aw.monma.monmamain.MonMaInterface.ACCOUNTID;
import static de.aw.monma.monmamain.MonMaInterface.ACTION;

/**
 * Uebersicht ueber alle nicht-ausgeblendeten Depots.
 */
public class FragmentDepots extends AWItemListRecyclerViewFragment<WertpapierBestand> implements TableColums {
    private static final int layout = R.layout.awlib_default_recycler_view;
    private static final int viewHolderLayout = R.layout.depotuebersicht_item;
    private static final DBDefinition tbd = DBDefinition.WPBestand;
    private static final String[] projection = new String[] { column_accountID, column_accountname, _id,
            AbstractDBHelper.SQLSumItem(column_marktwert), AbstractDBHelper.SQLSumItem(column_bruttoumsatz) };
    private final static String selection = " NOT " + column_ausgeblendet;
    private static String orderBy = column_accountID;
    private static String groupBy = column_accountID;

    @Override
    protected AWItemListAdapter<WertpapierBestand> createListAdapter() {
        AWItemListAdapter<WertpapierBestand> adapter = new AWItemListAdapter<WertpapierBestand>(this) {
            @Override
            protected long getID(@NonNull WertpapierBestand item) {
                return item.getID();
            }
        };
        adapter.setHasStableIds(true);
        return adapter;
    }

    @Override
    public void onAttach(Context activity) {
        super.onAttach(activity);
        startOrRestartLoader(layout, args);
    }

    @Override
    public void onBindViewHolder(AWLibViewHolder holder, WertpapierBestand item, int position) {
        holder.setVariable(BR.wpbestand, item);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        super.onLoadFinished(loader, cursor);
        List<WertpapierBestand> list = new ArrayList<>();
        if (cursor.moveToFirst()) {
            WertpapierBestand summenItem = new WertpapierBestand(getString(R.string.tvMarktwert));
            list.add(summenItem);
            long marktwert = 0;
            long bruttoumsatz = 0;
            do {
                WertpapierBestand item = new WertpapierBestand(tbd, cursor);
                list.add(item);
                bruttoumsatz += item.getBruttoumsatz();
                marktwert += item.getMarktwert();
            } while (cursor.moveToNext());
            summenItem.setMarktwert(marktwert);
            summenItem.setBruttoumsatz(bruttoumsatz);
        }
        getAdapter().swap(list);
    }

    @Override
    public void onRecyclerItemClick(View v, int position, WertpapierBestand item) {
        Intent intent = new Intent(getActivity(), ActivityWertpapiere.class);
        intent.putExtra(ACCOUNTID, item.getAccountID());
        intent.putExtra(ACTION, (Parcelable) MonMaInterface.Action.ShowWPUebersicht);
        startActivity(intent);
        super.onRecyclerItemClick(v, position, item);
    }

    @Override
    public boolean onRecyclerItemLongClick(View v, int position, WertpapierBestand item) {
        Intent intent = new Intent(getActivity(), ActivityWertpapiere.class);
        intent.putExtra(ACCOUNTID, item.getAccountID());
        intent.putExtra(ACTION, (Parcelable) MonMaInterface.Action.ShowBuchungen);
        startActivity(intent);
        return super.onRecyclerItemLongClick(v, position, item);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putString(SELECTION, selection);
        args.putStringArray(PROJECTION, projection);
        args.putString(GROUPBY, groupBy);
        args.putString(ORDERBY, orderBy);
        args.putInt(LAYOUT, layout);
        args.putInt(VIEWHOLDERLAYOUT, viewHolderLayout);
        args.putParcelable(DBDEFINITION, tbd);
    }
}