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.reports; 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.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.CheckedTextView; import java.util.ArrayList; import java.util.List; 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.database.DBHelper; import de.aw.monma.gv.Categorie; import de.aw.monma.gv.ReportStammdaten; import static de.aw.monma.database.TableColums.column_catclassID; import static de.aw.monma.database.TableColums.column_excl_cat; import static de.aw.monma.database.TableColums.column_excl_catclass; import static de.aw.monma.monmamain.MonMaInterface.REPORTSTAMMDATEN; /** * Liste der Kategorien fuer einen Report */ public class FragmentSelectionListCat extends AWItemListRecyclerViewFragment<Categorie> { private static final DBDefinition tbd = DBDefinition.Type_Cat; private static final int viewHolderLayout = R.layout.report_selection_list_cat_item; private static final String[] projection = tbd.getTableColumns(); private final List<Long> excludedItems = new ArrayList<>(); private ReportStammdaten mReportStammdaten; /** * Erstellt eine neue Categorie-Selectionsliste. * * @param stammdaten * Reportstammdaten * * @return Fragment */ public static FragmentSelectionListCat newInstance(ReportStammdaten stammdaten) { Bundle args = new Bundle(); FragmentSelectionListCat f = new FragmentSelectionListCat(); args.putParcelable(REPORTSTAMMDATEN, stammdaten); f.setArguments(args); return f; } @Override protected AWItemListAdapterTemplate<Categorie> createListAdapter() { AWItemListAdapter<Categorie> adapter = new AWItemListAdapter<Categorie>(this) { @Override protected long getID(@NonNull Categorie item) { return item.getID(); } }; adapter.setHasStableIds(true); return adapter; } @Override public void onAttach(Context activity) { super.onAttach(activity); mReportStammdaten = args.getParcelable(REPORTSTAMMDATEN); assert mReportStammdaten != null; String exc = mReportStammdaten.getAsString(column_excl_cat); if (exc != null) { String[] escItems = exc.split(","); for (String escItem : escItems) { excludedItems.add(Long.parseLong(escItem.trim())); } } String excludedClasses = mReportStammdaten.getAsString(column_excl_catclass); if (excludedClasses != null) { String selection = column_catclassID + " NOT IN (" + excludedClasses + ")"; args.putString(SELECTION, selection); } startOrRestartLoader(); } @Override public void onBindViewHolder(AWLibViewHolder holder, Categorie item, int position) { item.setAusgeblendet(excludedItems.contains(item.getID())); holder.setVariable(BR.cat, item); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.actionmode_selectionlist, menu); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { super.onLoadFinished(loader, cursor); List<Categorie> list = new ArrayList<>(); if (cursor.moveToFirst()) { do { Categorie item = new Categorie(cursor); list.add(item); } while (cursor.moveToNext()); } getAdapter().swap(list); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_selectAll: excludedItems.clear(); mReportStammdaten.setExcludedCategories(excludedItems); startOrRestartLoader(); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onPause() { super.onPause(); mReportStammdaten.setExcludedCategories(excludedItems); mReportStammdaten.update(DBHelper.getInstance()); } @Override public void onRecyclerItemClick(View v, int position, Categorie item) { CheckedTextView cb = v.findViewById(R.id.tvSelection_item); cb.toggle(); boolean ausgeblendet = !cb.isChecked(); if (!ausgeblendet) { excludedItems.remove(item.getID()); } else { excludedItems.add(item.getID()); } item.setAusgeblendet(ausgeblendet); if (item.isSuperCat()) { int pos = position + 1; Categorie cat = getAdapter().get(pos); while (cat.isSuperCatMember(item.getID())) { if (!ausgeblendet) { excludedItems.remove(cat.getID()); } else { excludedItems.add(cat.getID()); } cat.setAusgeblendet(ausgeblendet); pos++; cat = getAdapter().get(pos); } } super.onRecyclerItemClick(v, position, item); } @Override public void onResume() { super.onResume(); setTitle(R.string.menu_item_reportEditCat); } @Override protected void setInternalArguments(Bundle args) { super.setInternalArguments(args); args.putParcelable(DBDEFINITION, tbd); args.putStringArray(PROJECTION, projection); args.putInt(VIEWHOLDERLAYOUT, viewHolderLayout); } }