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.wertpapier; import android.app.SearchManager; 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.design.widget.FloatingActionButton; import android.support.v4.content.Loader; import android.support.v4.view.MenuItemCompat; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.PopupMenu; import de.aw.awlib.activities.AWMainActivity; 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.BR; import de.aw.monma.R; import de.aw.monma.database.DBDefinition; import de.aw.monma.databinding.WertpapierListItem; import de.aw.monma.enums.WPUmsatzArt; import de.aw.monma.gv.Account; import de.aw.monma.gv.PartnerStammdaten; import de.aw.monma.monmamain.MonMaInterface; import de.aw.monma.monmamain.MonMaResultCodes; import de.aw.monma.wertpapier.action.ActivityWPUmsatz; /** * Zeigt eine Liste der Wertpapierbuchungen. Bei Auswahl einer WPAction (Kauf, EInlieferung, * Ausgabe) wird ueber den Intent im Menu die ActivityWertpapiere mit Action DoWertpapierUmsatz * gestartet */ public class FragmentWertpapierBuchungenListe extends AWItemListRecyclerViewFragment<WertpapierListItem> implements MonMaInterface, MonMaResultCodes, SearchView.OnQueryTextListener, View.OnClickListener, AWItemListAdapterTemplate.ItemGenerator<WertpapierListItem> { protected static final DBDefinition tbd = DBDefinition.WPUMSATZView; private static final String TITLE = "TITLE"; private static final String accountIDSselection = column_accountID + " = ?"; private final static int detailLayout = R.layout.wpbuchungen_item; private static final String wpIDSselection = column_wpid + " = ?"; String orderBy = column_btag + " DESC, " + column_bruttoumsatz; private Integer mAccountID; private long mWPID; /** * Erstellt eine neues FragmentWertpapierbuchungen. Es duerfen nicht beide Parameter * gleichzeitig null sein. * * @param accountID * accountID, * @param wpID * wpID * @param wpName * wpName - kann null sein, wenn wpid = NOID * * @return Fragment */ public static FragmentWertpapierBuchungenListe newInstance(Integer accountID, Long wpID, String wpName) { if (accountID == null) { accountID = NOID; } if (wpID == null) { wpID = (long) NOID; } FragmentWertpapierBuchungenListe f = new FragmentWertpapierBuchungenListe(); String selection = column_partnername + " LIKE ? AND "; String[] selectionArgs = null; String title = null; if (wpID == NOID & accountID == NOID) { throw new IllegalStateException("WPID und ACCOUNTID sind Null!"); } if (wpID != NOID & accountID == NOID) { selection = wpIDSselection; selectionArgs = new String[] { String.valueOf(wpID) }; title = wpName; } if (wpID == NOID & accountID != NOID) { selection += accountIDSselection; selectionArgs = new String[] { "%%", String.valueOf(accountID) }; title = Account.getAccountName(accountID); } if (wpID != NOID & accountID != NOID) { selection = wpIDSselection + " AND " + accountIDSselection; selectionArgs = new String[] { String.valueOf(wpID), String.valueOf(accountID) }; title = Account.getAccountName(accountID) + " / " + wpName; } Bundle args = new Bundle(); args.putInt(ACCOUNTID, accountID); args.putLong(WPID, wpID); args.putString(WPNAME, wpName); args.putString(SELECTION, selection); args.putStringArray(SELECTIONARGS, selectionArgs); args.putString(TITLE, title); f.setArguments(args); return f; } /** * Wird gerufen, wenn Daten zu einem bestimmten Report angezeigt werden sollen. * * @param reportID * ReportID * @param wpid * WertpapierID * @param wpname * Name des WP * * @return Neues Fragment */ public static FragmentWertpapierBuchungenListe newInstance(long wpid, String wpname, long reportID) { FragmentWertpapierBuchungenListe f = FragmentWertpapierBuchungenListe.newInstance(NOID, wpid, wpname); Bundle args = f.getArguments(); args.putLong(REPORTID, reportID); args.putParcelable(DBDEFINITION, DBDefinition.ReportGeldfluss); String selection = args.getString(SELECTION); String title = args.getString(TITLE); args.putString(TITLE, title); args.putString(SELECTION, selection); return f; } @Override public WertpapierListItem createItem(Cursor c) { return new WertpapierListItem(c); } @Override protected AWItemListAdapterTemplate<WertpapierListItem> createListAdapter() { AWItemListAdapter<WertpapierListItem> adapter = new AWItemListAdapter<WertpapierListItem>(this) { @Override protected long getID(@NonNull WertpapierListItem item) { return item.getID(); } }; adapter.setHasStableIds(true); return adapter; } public int getAccountID() { return mAccountID; } @Override public void onAttach(Context activity) { super.onAttach(activity); startOrRestartLoader(); } @Override public void onBindViewHolder(AWLibViewHolder holder, WertpapierListItem item, int position) { holder.setVariable(BR.wpumsatz, item); holder.setHandler(BR.handler, this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.awlib_defaultFAB: PopupMenu popUpMenu = new PopupMenu(getActivity(), v); Menu menu = popUpMenu.getMenu(); // Aufbau des zugehoerigen Submenues for (WPUmsatzArt umsatzArt : WPUmsatzArt.values()) { switch (umsatzArt) { case Kauf: case OthrInc: case WP_Einlage: case Ausgabe: Intent intent = new Intent(getActivity(), ActivityWPUmsatz.class); intent.putExtra(ACTION, (Parcelable) Action.DoWertpapierUmsatz); intent.putExtra(ACCOUNTID, mAccountID); intent.putExtra(WPUMSATZART, umsatzArt); MenuItem item = menu.add(Menu.NONE, umsatzArt.getTextResID(), Menu.NONE, umsatzArt.getBeschreibungID()); item.setIntent(intent); } } popUpMenu.show(); break; default: } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); mAccountID = args.getInt(ACCOUNTID, NOID); mWPID = args.getLong(WPID, NOID); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.fragment_wp_buchungen, menu); if (mWPID == NOID) { // SearchView nur zeigen, wenn nicht auf ein WP eingeschraenkt MenuItem item = menu.findItem(R.id.menu_List_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); searchView.setOnQueryTextListener(this); item.setVisible(true); } } /** * Subtitle aus ActionBar entfernen */ @Override public void onDestroy() { super.onDestroy(); setTitle(null); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { super.onLoadFinished(loader, cursor); getAdapter().swap(cursor, this); } @Override public void onPause() { super.onPause(); FloatingActionButton fab = ((AWMainActivity) getActivity()).getDefaultFAB(); fab.setVisibility(View.GONE); fab.setOnClickListener(null); } @Override public boolean onQueryTextChange(String newText) { String[] selectionArgs = args.getStringArray(SELECTIONARGS); assert selectionArgs != null; selectionArgs[0] = "%" + newText + "%"; args.putStringArray(SELECTIONARGS, selectionArgs); startOrRestartLoader(layout, args); return true; } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public void onRecyclerItemClick(View v, int position, WertpapierListItem item) { Intent intent = new Intent(getContext(), ActivityWertpapiere.class); intent.putExtra(ACTION, (Parcelable) Action.ShowWertpapierUmsatz); intent.putExtra(ID, item.getID()); startActivity(intent); super.onRecyclerItemClick(v, position, item); } @Override public boolean onRecyclerItemLongClick(View v, int position, WertpapierListItem item) { WPUmsatzArt wpumsArt = item.getWPUmsatzArt(); switch (wpumsArt) { case Verkauf: case WP_Entnahme: FragmentWPPaketListe f = FragmentWPPaketListe.newInstance(item.getID()); replaceFragment(f); Long partnerID = item.getPartnerID(); PartnerStammdaten partner = new PartnerStammdaten(partnerID); String title = getString(R.string.Pakete) + " " + wpumsArt + " " + partner.getPartnerName(); setTitle(title); return true; } return super.onRecyclerItemLongClick(v, position, item); } @Override public void onResume() { super.onResume(); FloatingActionButton fab = ((AWMainActivity) getActivity()).getDefaultFAB(); fab.setVisibility(View.VISIBLE); fab.setOnClickListener(this); setTitle(args.getString(TITLE)); } @Override protected void setInternalArguments(Bundle args) { super.setInternalArguments(args); args.putInt(VIEWHOLDERLAYOUT, detailLayout); args.putParcelable(DBDEFINITION, tbd); args.putString(ORDERBY, orderBy); } }