eowise / recyclerview-stickyheaders

DEPRECATED. Android library that integrate sticky section headers in your RecyclerView
http://eowise.github.io/recyclerview-stickyheaders
MIT License
961 stars 148 forks source link

Using CursorAdapter returns "Adapter must have stable ids" error #45

Open ethpran opened 9 years ago

ethpran commented 9 years ago

I'm not positive if I'm doing something incorrect here. My CursorAdapter uses the primary column id as the adapter id and I override setHasStableIds like so:

private Cursor mCursor;

private boolean mDataValid;

private int mRowIdColumn;

private DataSetObserver mDataSetObserver;

public CursorRecyclerViewAdapter(Cursor cursor) {
    mCursor = cursor;
    mDataValid = cursor != null;
    mRowIdColumn = mDataValid ? mCursor.getColumnIndex("_id") : -1;
    mDataSetObserver = new NotifyingDataSetObserver();
    if (mCursor != null) {
        mCursor.registerDataSetObserver(mDataSetObserver);
    }
}

...

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

@Override
public void setHasStableIds(boolean hasStableIds) {
    super.setHasStableIds(true);
}

However, I keep getting the above error message when trying to implement your library. Is there some kind of problem with using a CursorAdapter as the underlying adapter for the RecyclerView? Perhaps I'm implementing this incorrectly? Thanks for your attention

LorneLaliberte commented 9 years ago

Are you calling setHasStableIds(true) on your adapter? Otherwise mHasStableIds will still be false.

If your adapter always has stable ids, you can simply set it from the constructor:

public CursorRecyclerViewAdapter(Cursor cursor) {
    mCursor = cursor;
    mDataValid = cursor != null;
    mRowIdColumn = mDataValid ? mCursor.getColumnIndex("_id") : -1;
    mDataSetObserver = new NotifyingDataSetObserver();
    if (mCursor != null) {
        mCursor.registerDataSetObserver(mDataSetObserver);
    }
    setHasStableIds(true);
}

You do not actually have to override setHasStableIds() at all.