fr.eoit.activity.fragment.ItemMaterialListFragment.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.fragment.ItemMaterialListFragment.java

Source

/*
 * 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;

import android.content.ContentUris;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;

import fr.eoit.EOITConst;
import fr.eoit.R;
import fr.eoit.activity.util.ItemListViewBinder;
import fr.eoit.activity.util.ItemListViewBinder.RedQuantityBehavior;
import fr.eoit.db.bean.*;

/**
 * @author picon.software
 *
 */
public class ItemMaterialListFragment extends ItemListFragment<SimpleCursorAdapter> {

    protected String[] dataColumns = { ItemMaterials.COLUMN_NAME_MATERIAL_ITEM_ID, Item.COLUMN_NAME_NAME,
            ItemMaterials.COLUMN_NAME_QUANTITY, Item.COLUMN_NAME_CHOSEN_PRICE_ID };
    protected int[] viewIDs = { R.id.ITEM_ICON, R.id.ITEM_NAME, R.id.ITEM_QUANTITY, R.id.WARN_ICON };
    protected RedQuantityBehavior behavior;

    protected long itemId;
    protected long categoryId;

    protected int elementLayoutId = R.layout.item_row_small;

    private SimpleCursorAdapter adapter;

    public ItemMaterialListFragment() {
        super();
        sectionTitleId = -1;
    }

    /**
     * @return the itemId
     */
    public long getItemId() {
        return itemId;
    }

    /**
     * @param itemId
     *            the itemId to set
     */
    public void setItemId(long itemId) {
        this.itemId = itemId;
        initOrRestart();
    }

    /**
     * @return the categoryId
     */
    public long getCategoryId() {
        return categoryId;
    }

    /**
     * @param categoryId the categoryId to set
     */
    public void setCategoryId(long categoryId) {
        this.categoryId = categoryId;
        if (categoryId == EOITConst.Categories.ASTEROID_CATEGORIE_ID) {
            behavior = RedQuantityBehavior.WITH_REFINE_QUANTITY;
        } else if (categoryId == EOITConst.Categories.PLANETARY_COMMODITIES_CATEGORIE_ID) {
            behavior = RedQuantityBehavior.NONE;
        } else {
            behavior = RedQuantityBehavior.WITH_WASTE_QUANTITY;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        if (adapter != null)
            adapter.swapCursor(null);
    }

    @Override
    protected SimpleCursorAdapter adapteurInit() {
        return new SimpleCursorAdapter(getActivity(), elementLayoutId, null, dataColumns, viewIDs,
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    }

    @Override
    protected Loader<Cursor> getCursorLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(),
                ContentUris.withAppendedId(ItemMaterials.CONTENT_ITEM_ID_URI_BASE_PRICES, itemId),
                new String[] { "i." + Item._ID + " AS " + Item._ID, ItemMaterials.COLUMN_NAME_MATERIAL_ITEM_ID,
                        Item.COLUMN_NAME_NAME, Item.COLUMN_NAME_CHOSEN_PRICE_ID, Item.COLUMN_NAME_GROUP_ID,
                        Prices.COLUMN_NAME_BUY_PRICE, Prices.COLUMN_NAME_OWN_PRICE, Prices.COLUMN_NAME_SELL_PRICE,
                        Prices.COLUMN_NAME_PRODUCE_PRICE, ItemMaterials.COLUMN_NAME_QUANTITY,
                        Blueprint.COLUMN_NAME_ML },
                ItemMaterials.COLUMN_NAME_ACTIVITY_ID + " IS NULL AND " + Groups.COLUMN_NAME_CATEGORIE_ID
                        + " NOT IN (" + EOITConst.Categories.SKILL_CATEGORIE_ID + ")",
                null, "i." + Item._ID + " ASC");
    }

    @Override
    protected void onLoadFinishedAdapteur(Cursor cursor, SimpleCursorAdapter adapter) {
        adapter.setViewBinder(new ItemListViewBinder(behavior));
        adapter.swapCursor(cursor);
    }
}