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.loader; import android.app.Activity; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import fr.eoit.EOITConst; import fr.eoit.activity.util.ItemUtil; import fr.eoit.bean.ItemBeanWithMaterials; import fr.eoit.db.bean.Blueprint; import fr.eoit.db.bean.Groups; import fr.eoit.db.bean.Item; import fr.eoit.db.bean.ItemMaterials; import fr.eoit.db.bean.Prices; import fr.eoit.util.SparseItemBeanArray; import fr.eoit.util.manufacture.ManufactureUtils; import fr.piconsoft.db.util.DbUtil; /** * @author picon.software * */ public class BaseProductionNeedsLoader extends AsyncTaskLoader<SparseItemBeanArray> { private Activity activity; private SparseItemBeanArray data; private int itemId, categorieId; public BaseProductionNeedsLoader(Activity activity, int itemid, int categorieId) { super(activity); this.itemId = itemid; this.activity = activity; this.categorieId = categorieId; } @Override public SparseItemBeanArray loadInBackground() { data = getBaseProductionNeeds(activity, itemId, categorieId, 1); return data; } @Override protected void onStartLoading() { if (data == null) { forceLoad(); } else { deliverResult(data); } } public static SparseItemBeanArray getBaseProductionNeeds(Context context, long itemId, long categorieId, long numberOfItems) { final Cursor cursorItemMaterials = context.getContentResolver().query( ContentUris.withAppendedId(ItemMaterials.CONTENT_ITEM_ID_URI_BASE_PRICES, itemId), new String[] { ItemMaterials._ID, ItemMaterials.COLUMN_NAME_MATERIAL_ITEM_ID, ItemMaterials.COLUMN_NAME_DAMAGE_PER_JOB, ItemMaterials.COLUMN_NAME_QUANTITY, Item.COLUMN_NAME_NAME, Item.COLUMN_NAME_VOLUME, Item.COLUMN_NAME_CHOSEN_PRICE_ID, Blueprint.COLUMN_NAME_ML, Blueprint.COLUMN_NAME_MATERIAL_UNIT_PER_BATCH, Prices.COLUMN_NAME_BUY_PRICE, Prices.COLUMN_NAME_OWN_PRICE, Prices.COLUMN_NAME_SELL_PRICE, Prices.COLUMN_NAME_PRODUCE_PRICE, Groups.COLUMN_NAME_CATEGORIE_ID }, //null, "(" + ItemMaterials.COLUMN_NAME_ACTIVITY_ID + " = 1 OR " + ItemMaterials.COLUMN_NAME_ACTIVITY_ID + " IS NULL) AND " + Groups.COLUMN_NAME_CATEGORIE_ID + " NOT IN ( " + EOITConst.Categories.SKILL_CATEGORIE_ID + ")", null, null); SparseItemBeanArray items = new SparseItemBeanArray(); try { if (DbUtil.hasAtLeastOneRow(cursorItemMaterials)) { while (!cursorItemMaterials.isAfterLast()) { long id = cursorItemMaterials.getInt( cursorItemMaterials.getColumnIndexOrThrow(ItemMaterials.COLUMN_NAME_MATERIAL_ITEM_ID)); int chosenPrice = cursorItemMaterials .getInt(cursorItemMaterials.getColumnIndexOrThrow(Item.COLUMN_NAME_CHOSEN_PRICE_ID)); ItemBeanWithMaterials item = ItemUtil.getItemWithPrices(cursorItemMaterials, numberOfItems, categorieId); items = ManufactureUtils.addItemToMap(items, item, numberOfItems, null); if (chosenPrice == EOITConst.PRODUCE_PRICE_ID) { item.materials = getBaseProductionNeeds(context, id, categorieId, numberOfItems); } cursorItemMaterials.moveToNext(); } } } finally { cursorItemMaterials.close(); } return items; } public static class BaseProductionNeedsLoaderCallBacks implements LoaderCallbacks<SparseItemBeanArray> { private Activity activity; private int itemId, categorieId; private OnLoadFinishedListener listener; public BaseProductionNeedsLoaderCallBacks(Activity activity, OnLoadFinishedListener listener, int itemId, int categorieId) { this.activity = activity; this.itemId = itemId; this.categorieId = categorieId; this.listener = listener; } @Override public Loader<SparseItemBeanArray> onCreateLoader(int id, Bundle args) { return new BaseProductionNeedsLoader(activity, itemId, categorieId); } @Override public void onLoadFinished(Loader<SparseItemBeanArray> loader, SparseItemBeanArray data) { listener.onLoadFinishedBaseProductionNeeds(itemId, data); } @Override public void onLoaderReset(Loader<SparseItemBeanArray> loader) { } } public interface OnLoadFinishedListener { public void onLoadFinishedBaseProductionNeeds(int itemId, SparseItemBeanArray data); } }