Pagination in recyclerview using cursor adapter

时间:2016-04-07 10:39:08

标签: android sqlite cursor android-recyclerview ormlite

Hello i want to do pagination using sqlite database,I am able to get results in sets of 100 rows/query. I am using cursor adapter here. Data is getting replaced instead of getting appended. Below is my Cursor Adapter for recyclerview

package com.example.example.util;


import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.support.v7.widget.RecyclerView;

// Code from https://gist.github.com/skyfishjy/443b7448f59be978bc59
// Fix to remove observers (when using CursorLoaders) by: https://gist.github.com/quanturium/46541c81aae2a916e31d
public abstract class CursorRecyclerViewAdapter<VH extends  RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

private Cursor mCursor;
private boolean mDataValid;
private int mRowIdColumn;

public CursorRecyclerViewAdapter(Cursor cursor) {
    mCursor = cursor;
    mDataValid = cursor != null;
    mRowIdColumn = mDataValid ? mCursor.getColumnIndexOrThrow("NAMEID") : -1;
    setHasStableIds(true);
}

public Cursor getCursor() {
    return mCursor;
}

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

@Override
public long getItemId(int position) {
    if (hasStableIds() && mDataValid && mCursor != null) {
        if (mCursor.moveToPosition(position)) {
            return mCursor.getLong(mRowIdColumn);
        } else {
            return RecyclerView.NO_ID;
        }
    } else {
        return RecyclerView.NO_ID;
    }
}

public Object getItem(int position) {
    if (mDataValid && mCursor != null) {
        mCursor.moveToPosition(position);
        return mCursor;
    } else {
        return null;
    }
}

public abstract void onBindViewHolder(VH viewHolder, Cursor cursor);

@Override
public void onBindViewHolder(VH viewHolder, int position) {
    if (!mDataValid) {
        throw new IllegalStateException("this should only be called when the cursor is valid");
    }
    if (!mCursor.moveToPosition(position)) {
        throw new IllegalStateException("couldn't move cursor to position " + position);
    }
    onBindViewHolder(viewHolder, mCursor);
}

/**
 * Change the underlying cursor to a new cursor. If there is an existing cursor it will be
 * closed.
 */
public void changeCursor(Cursor cursor) {
    Cursor old = swapCursor(cursor);
    if (old != null) {
        old.close();
    }
}

/**
 * Swap in a new Cursor, returning the old Cursor.  Unlike
 * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
 * closed.
 */
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == mCursor) {
        return null;
    }
    final int oldItemCount = getItemCount();
    final Cursor oldCursor = mCursor;

    mCursor = newCursor;
    if (mCursor != null) {
        mRowIdColumn = newCursor.getColumnIndexOrThrow("NAMEID");
        mDataValid = true;
        notifyDataSetChanged();
    } else {
        mRowIdColumn = -1;
        mDataValid = false;
        // notify the observers about the lack of a data set
        notifyItemRangeRemoved(0, oldItemCount);
    }

    return oldCursor;
}

}

any suggestion what must be going wrong?

0 个答案:

没有答案