com.bydavy.card.receipts.ReceiptListAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.bydavy.card.receipts.ReceiptListAdapter.java

Source

/*
 * Copyright (C) 2012 Davy Leggieri
 * 
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package com.bydavy.card.receipts;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.bydavy.card.receipts.providers.Receipts;
import com.bydavy.card.receipts.ui.ReceiptListItemView;
import com.bydavy.card.receipts.utils.CollectionHelper;

public class ReceiptListAdapter extends ResourceCursorAdapter {

    private static final String STATE_SELECTED_IDS = "rl_adapter_selected_ids";

    public interface Callback {
        /** Called when the user selects/unselects a receipt */
        void onAdapterSelectedChanged(boolean newSelected, int mSelectedCount);
    }

    private final HashSet<Long> mSelectedSet = CollectionHelper.newHashSet();
    private final Callback mCallback;
    /* Cache of column index */
    private int mColumnId;
    private int mColumnShop;
    private int mColumnNote;
    private int mColumnTotal;
    private int mColumnDate;
    private int mColumnVerified;

    @SuppressWarnings("deprecation")
    public ReceiptListAdapter(Context context, int layout, Cursor cursor, Callback callback) {
        super(context, layout, cursor);
        cacheColumnIndex(cursor);
        mCallback = callback;
    }

    private void cacheColumnIndex(Cursor c) {
        if (c != null) {
            mColumnId = c.getColumnIndexOrThrow(Receipts._ID);
            mColumnShop = c.getColumnIndexOrThrow(Receipts.COLUMN_NAME_SHOP);
            mColumnNote = c.getColumnIndexOrThrow(Receipts.COLUMN_NAME_NOTE);
            mColumnTotal = c.getColumnIndexOrThrow(Receipts.COLUMN_NAME_TOTAL);
            mColumnDate = c.getColumnIndexOrThrow(Receipts.COLUMN_NAME_DATE);
            mColumnVerified = c.getColumnIndexOrThrow(Receipts.COLUMN_NAME_VERIFIED);
        }
    }

    @Override
    public Cursor swapCursor(Cursor newCursor) {
        cacheColumnIndex(newCursor);
        return super.swapCursor(newCursor);
    }

    public Set<Long> getSelectedSet() {
        return mSelectedSet;
    }

    public int getSelectedCount() {
        return mSelectedSet.size();
    }

    public boolean isSelected(ReceiptListItemView view) {
        return isSelected(view.mReceiptId);
    }

    public boolean isSelected(long receiptId) {
        return mSelectedSet.contains(receiptId);
    }

    public void toggleSelected(ReceiptListItemView view) {
        final boolean wasSelected = isSelected(view);
        if (wasSelected) {
            mSelectedSet.remove(view.mReceiptId);
        } else {
            mSelectedSet.add(view.mReceiptId);
        }
        if (mCallback != null) {
            mCallback.onAdapterSelectedChanged(!wasSelected, getSelectedCount());
        }
    }

    public void clearSelected() {
        if (mSelectedSet.size() > 0) {
            mSelectedSet.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final ReceiptListItemView view = (ReceiptListItemView) super.newView(context, cursor, parent);
        view.bindViewInit(this);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final ReceiptListItemView receiptListItem = (ReceiptListItemView) view;

        receiptListItem.mReceiptId = cursor.getLong(mColumnId);
        receiptListItem.setShop(cursor.getString(mColumnShop));
        receiptListItem.setNote(cursor.getString(mColumnNote));
        receiptListItem.setTotal(cursor.getFloat(mColumnTotal));
        receiptListItem.setTimeStamp(cursor.getInt(mColumnDate));
        receiptListItem.setIsVerified(cursor.getInt(mColumnVerified) == 1);
        receiptListItem.setIsSelected(isSelected(receiptListItem.mReceiptId));
    };

    public void save(Bundle outState) {
        final long[] arrayIds = new long[mSelectedSet.size()];
        int i = 0;
        for (final Long id : mSelectedSet) {
            arrayIds[i++] = id;
        }
        outState.putLongArray(STATE_SELECTED_IDS, arrayIds);
    }

    public void load(Bundle savedState) {
        mSelectedSet.clear();
        for (final long id : savedState.getLongArray(STATE_SELECTED_IDS)) {
            mSelectedSet.add(id);
            if (mCallback != null) {
                mCallback.onAdapterSelectedChanged(true, getSelectedCount());
            }
        }
        notifyDataSetChanged();
    }
}