de.aw.monma.wertpapier.FragmentWertpapierKurseListe.java Source code

Java tutorial

Introduction

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

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.View;

import com.android.databinding.library.baseAdapters.BR;

import de.aw.awlib.adapters.AWItemListAdapter;
import de.aw.awlib.adapters.AWItemListAdapterTemplate;
import de.aw.awlib.recyclerview.AWItemListRecyclerViewFragment;
import de.aw.awlib.recyclerview.AWLibViewHolder;
import de.aw.monma.R;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.gv.WPKurs;
import de.aw.monma.gv.WertpapierBestand;
import de.aw.monma.monmamain.MonMaInterface;
import de.aw.monma.wertpapier.dialoge.DialogKursaktualisierung;

/**
 * @author alex
 */
public class FragmentWertpapierKurseListe extends AWItemListRecyclerViewFragment<WPKurs>
        implements MonMaInterface, AWItemListAdapterTemplate.ItemGenerator<WPKurs> {
    private static final int viewHolderLayout = R.layout.wertpapierkurse_item;
    private static final DBDefinition tbd = DBDefinition.WPKurse;
    private static final String selection = column_wpid + " = ?";
    private static final String orderBy = column_btag + " DESC";

    public static FragmentWertpapierKurseListe newInstance(WertpapierBestand item) {
        Bundle args = new Bundle();
        args.putLong(WPID, item.getID());
        args.putString(WPNAME, item.getWPName());
        FragmentWertpapierKurseListe fragment = new FragmentWertpapierKurseListe();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public WPKurs createItem(Cursor c) {
        return new WPKurs(c);
    }

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

    @Override
    public LayoutManager getLayoutManager() {
        return new GridLayoutManager(getActivity(), 2);
    }

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

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        super.onLoadFinished(loader, cursor);
        getAdapter().swap(cursor, this);
    }

    @Override
    public void onRecyclerItemClick(View v, int position, WPKurs item) {
        super.onRecyclerItemClick(v, position, item);
        item.setWPName(args.getString(WPNAME));
        DialogKursaktualisierung dialog = DialogKursaktualisierung.newInstance(item);
        dialog.show(getFragmentManager(), dialog.TAG);
    }

    @Override
    public void onResume() {
        super.onResume();
        setTitle(args.getString(WPNAME));
        setSubTitle(R.string.tvKursFuerWertpapier);
    }

    @Override
    public void setCursorLoaderArguments(int id, Bundle args) {
        args.putString(ORDERBY, orderBy);
        args.putString(SELECTION, selection);
        Long wpid = args.getLong(WPID);
        String[] selectionArgs = new String[] { String.valueOf(wpid) };
        args.putStringArray(SELECTIONARGS, selectionArgs);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putInt(LAYOUT, layout);
        args.putInt(VIEWHOLDERLAYOUT, viewHolderLayout);
        args.putParcelable(DBDEFINITION, tbd);
    }
}