fr.eoidb.activity.util.ItemListViewBinder.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoidb.activity.util.ItemListViewBinder.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.eoidb.activity.util;

import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;

import fr.eoidb.R;
import fr.eoidb.util.IconUtil;

/**
 * @author picon.software
 *
 */
public class ItemListViewBinder implements SimpleCursorAdapter.ViewBinder, ViewBinder {

    public static enum RedQuantityBehavior {
        NONE, WITH_WASTE_QUANTITY, WITH_REFINE_QUANTITY, PRICE
    }

    private long id = 0;

    private double totalPrice = 0, totalVolume = 0;

    private int numberOfElements = -1;
    private int currentPosition = 0;

    public ItemListViewBinder() {
        super();
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();

        numberOfElements = cursor.getCount();
        currentPosition = cursor.getPosition();

        switch (viewId) {
        case R.id.item_name:
        case R.id.ITEM_NAME:
            String value = cursor.getString(columnIndex);
            initName(view, value);
            break;

        case R.id.item_icon:
        case R.id.ITEM_ICON:
            id = cursor.getLong(columnIndex);
            initIcon(view, id);
            break;

        default:
            throw new IllegalArgumentException("viewId : " + viewId);
        }

        return true;
    }

    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        int viewId = view.getId();

        switch (viewId) {
        case R.id.ITEM_NAME:
            String value = String.valueOf(data);
            initName(view, value);
            break;

        case R.id.ITEM_ICON:
            id = (Long) data;
            initIcon(view, id);
            break;

        default:
            throw new IllegalArgumentException("viewId : " + viewId);
        }

        return true;
    }

    private void initIcon(View view, long id) {
        ImageView imageView = (ImageView) view;
        IconUtil.initIcon(id, imageView);
    }

    private void initName(View view, String value) {
        TextView textView = (TextView) view;
        textView.setText(value);
    }

    protected void addToTotalPrice(long id, double price, long quantity) {
        totalPrice += (price * quantity);
    }

    protected void addToTotalVolume(long id, double volume, long quantity) {
        totalVolume += (volume * quantity);
    }

    /**
     * @return the totalPrice
     */
    public double getTotalPrice() {
        return totalPrice;
    }

    /**
     * @return the totalVolume
     */
    public double getTotalVolume() {
        return totalVolume;
    }

    /**
     * @return the numberOfElements
     */
    public int getNumberOfElements() {
        return numberOfElements;
    }

    /**
     * @return the currentPosition
     */
    public int getCurrentPosition() {
        return currentPosition;
    }

}