de.aw.monma.hbci.FragmentHBCIBanken.java Source code

Java tutorial

Introduction

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

import android.app.SearchManager;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;

import de.aw.awlib.recyclerview.AWCursorRecyclerViewFragment;
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.hbci.hbcigv.HBCIBank;
import de.aw.monma.monmamain.MonMaInterface;

/**
 * zeigt die Liste der HBCI-Banken. Bei Click wird {@link FragmentInitMonMaPassport} gestartet
 */
public class FragmentHBCIBanken extends AWCursorRecyclerViewFragment
        implements MonMaInterface, SearchView.OnQueryTextListener {
    private final static DBDefinition tbd = DBDefinition.HBCIBank;
    private final static String selection = column_hbci_blz + " LIKE ? OR " + column_hbci_bankname + " LIKE ? ";
    private static final String orderBy = column_hbci_blz;
    private static int layout = R.layout.awlib_default_recycler_view;
    @SuppressWarnings("FieldCanBeLocal")
    private static int viewHolderLayout = R.layout.hbcibank;
    private String[] selectionArgs = new String[] { "%%", "%%" };

    @Override
    public void onBindViewHolder(AWLibViewHolder holder, Cursor cursor, int position) {
        HBCIBank item = new HBCIBank(cursor);
        holder.setVariable(BR.hbcibank, item);
    }

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

    /**
     * In der ActionBar die SearchView initialisieren
     */
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_hbcibank, menu);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_List_search));
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setOnQueryTextListener(this);
        super.onCreateOptionsMenu(menu, inflater);
    }

    /**
     * Wird ein Wertpapier ueber das SearchWidget gesucht, wird bei jedem neuen Buchstaben die Liste
     * aktualisiert.
     *
     * @return immer true
     */
    @Override
    public boolean onQueryTextChange(String newText) {
        selectionArgs = new String[] { "%" + newText + "%", "%" + newText + "%" };
        args.putStringArray(SELECTIONARGS, selectionArgs);
        startOrRestartLoader(layout, args);
        return true;
    }

    /**
     * Wird ein Wertpapier ueber das SearchWidget gesucht, wird bei Click auf Oeffnen die Liste der
     * Buchungen angezeigt, wenn nur ganau ein WP angezeigt wird. Ansonsten wird nichts gemacht.
     *
     * @return immer true
     */
    @Override
    public boolean onQueryTextSubmit(String query) {
        return true;
    }

    /**
     * Bei Click wird entweder das Fragment informiert, welches als TargetFragment gesetzt ist oder
     * das contextmenu gezeigt. In den jeweiligen extras sind folgende Informationen enthalten:
     * WPID, AMOUNT, WPNAME des gewaehlten Wertpapiers.
     */
    @Override
    public void onRecyclerItemClick(View view, int position, long id) {
        super.onRecyclerItemClick(view, position, id);
        FragmentInitMonMaPassport f = FragmentInitMonMaPassport.newInstance(new HBCIBank(id));
        replaceFragment(f);
    }

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