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

Java tutorial

Introduction

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

Source

/*
 * Copyright (C) 2006 The Android Open Source Project
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bydavy.card.receipts;

import java.lang.ref.WeakReference;

import android.database.Cursor;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.bydavy.card.receipts.fragments.ReceiptFragment;
import com.bydavy.card.receipts.fragments.ReceiptListFragment;
import com.bydavy.card.receipts.providers.Receipts;
import com.bydavy.card.receipts.utils.LogHelper;

public class ReceiptPagerAdapter extends FragmentStatePagerAdapter {
    /** Change first statement of if condition to enable class debug **/
    private static final boolean DEBUG = LogHelper.DEBUG ? false : LogHelper.DEFAULT_DEBUG;
    private static final String DEBUG_PREFIX = ReceiptListFragment.class.getName();

    private Cursor mCursor;
    private int mRowIDColumn;
    private int mRowShopColumn;
    private boolean mDataValid;
    private WeakReference<Fragment> mPrimary;

    public ReceiptPagerAdapter(FragmentManager fm, Cursor cursor) {
        super(fm);
        changeCursor(cursor);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        // can cast safely (object can only be of type fragment)
        mPrimary = new WeakReference<Fragment>((Fragment) object);
    }

    public Fragment getPrimaryItem() {
        return mPrimary != null ? mPrimary.get() : null;
    }

    @Override
    public Fragment getItem(int position) {
        if (DEBUG) {
            LogHelper.d(DEBUG_PREFIX + ": getItem(" + position + ")");
        }
        return ReceiptFragment.newInstance(getItemId(position));
    }

    public long getItemId(int position) {
        if (!mDataValid || (mCursor == null)) {
            return 0;
        }
        mCursor.moveToPosition(position);
        return mCursor.getLong(mRowIDColumn);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        mCursor.moveToPosition(position);
        return mCursor.getString(mRowShopColumn);
    }

    @Override
    public int getCount() {
        if (!mDataValid || (mCursor == null)) {
            return 0;
        }
        return mCursor.getCount();
    }

    public final void changeCursor(Cursor cursor) {
        swapCursor(cursor);
    }

    @Override
    public int getItemPosition(Object object) {
        // Safe to cast, we have only one time of object
        final ReceiptFragment receiptFragment = (ReceiptFragment) object;
        receiptFragment.getId();
        // TODO find a way to know the new position of this fragment
        return POSITION_NONE;
    }

    public Cursor swapCursor(Cursor newCursor) {
        if (newCursor == mCursor) {
            return null;
        }
        final Cursor oldCursor = mCursor;
        mCursor = newCursor;
        if (newCursor != null) {
            mRowIDColumn = newCursor.getColumnIndexOrThrow(BaseColumns._ID);
            mRowShopColumn = newCursor.getColumnIndexOrThrow(Receipts.COLUMN_NAME_SHOP);
            mDataValid = true;
            // notify the observers about the new cursor
            notifyDataSetChanged();
        } else {
            mRowIDColumn = -1;
            mDataValid = false;
            // notify the observers about the lack of a data set
            notifyDataSetChanged();
        }

        return oldCursor;
    }
}