Java tutorial
/** * Copyright (C) 2012 Picon software * * 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 2 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, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package fr.eoit.activity.fragment.blueprint; import android.database.Cursor; import android.database.MatrixCursor; import android.os.Bundle; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import fr.eoit.EOITConst; import fr.eoit.R; import fr.eoit.activity.fragment.EnhancedMaterialListFragment; import fr.piconsoft.activity.fragment.LoaderFragment; import fr.eoit.activity.fragment.manufacture.ProductionPlanFragment.ProductionStep; import fr.eoit.activity.loader.InvestItemsLoader; import fr.eoit.activity.loader.InvestItemsLoader.OnLoadFinishedListener; import fr.eoit.bean.ItemBeanWithMaterials; import fr.eoit.db.bean.Stock; import fr.piconsoft.db.util.DbUtil; import fr.eoit.util.SparseItemBeanArray; import fr.eoit.util.manufacture.ManufactureUtils; import fr.piconsoft.db.util.InClauseQueryBuilder; import java.util.Set; import java.util.TreeSet; /** * @author picon.software * */ public class ItemInvestFragment extends LoaderFragment<Cursor> implements OnLoadFinishedListener { private static final int INVEST_ITEMS_LOADER_ID = EOITConst.getNextLoaderIdSequence(); private int itemId; private SparseItemBeanArray investItemsItemMap, stockItems = new SparseItemBeanArray(); private Set<Integer> ids = new TreeSet<Integer>(); private EnhancedMaterialListFragment investItemsFragment; /** * @param itemId the itemId to set */ public void setItemId(int itemId) { this.itemId = itemId; getLoaderManager().initLoader(INVEST_ITEMS_LOADER_ID, null, new InvestItemsLoader.InvestItemsLoaderCallBacks(getActivity(), this, itemId)); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragment = inflater.inflate(R.layout.blueprint_info_invest, container, false); investItemsFragment = (EnhancedMaterialListFragment) getFragmentManager() .findFragmentById(R.id.INVEST_ITEMS); return fragment; } @Override protected void onReload() { getLoaderManager().initLoader(INVEST_ITEMS_LOADER_ID, null, new InvestItemsLoader.InvestItemsLoaderCallBacks(getActivity(), this, itemId)); } @Override public void onLoadFinishedInvestItems(SparseItemBeanArray data) { this.investItemsItemMap = data; if (investItemsItemMap != null) { ids.clear(); for (ItemBeanWithMaterials item : investItemsItemMap.values()) { ids.addAll(ManufactureUtils.getIds(item)); } } initOrRestart(); } @Override protected Loader<Cursor> getCursorLoader(int id, Bundle args) { return new CursorLoader(getActivity(), Stock.CONTENT_URI, new String[] { Stock._ID, Stock.COLUMN_NAME_ITEM_ID, Stock.COLUMN_NAME_QUANTITY }, InClauseQueryBuilder.buildInClause(Stock.COLUMN_NAME_ITEM_ID, ids), null, Stock.COLUMN_NAME_ITEM_ID + " ASC"); } @Override public void onLoadFinished(Cursor cursor) { if (DbUtil.hasAtLeastOneRow(cursor)) { while (!cursor.isAfterLast()) { int id = cursor.getInt(cursor.getColumnIndexOrThrow(Stock.COLUMN_NAME_ITEM_ID)); long quantity = cursor.getLong(cursor.getColumnIndexOrThrow(Stock.COLUMN_NAME_QUANTITY)); ItemBeanWithMaterials item = new ItemBeanWithMaterials(); item.id = id; item.quantity = quantity; stockItems.put(id, item); cursor.moveToNext(); } } //SparseItemBeanArray remainingInvestItems = investItemsItemMap.exclude(stockItems); MatrixCursor investItemsMatrixCursor = EnhancedMaterialListFragment.getEmptyCursor(); for (ItemBeanWithMaterials item : investItemsItemMap.values()) { EnhancedMaterialListFragment.addRowToMatrixCursor(investItemsMatrixCursor, item); } investItemsFragment.initialize(new ProductionStep("INVEST", investItemsMatrixCursor)); } @Override public void onLoaderReset(Loader<Cursor> loader) { } }