Skip to content

Instantly share code, notes, and snippets.

@yakuzaaaa
Created August 7, 2016 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakuzaaaa/2aa174e6a03b676f764c944c8a0baa7c to your computer and use it in GitHub Desktop.
Save yakuzaaaa/2aa174e6a03b676f764c944c8a0baa7c to your computer and use it in GitHub Desktop.
A cursor adapter implementation for recycler views in android
import android.database.Cursor;
import android.database.DataSetObserver;
import android.support.v7.widget.RecyclerView;
/**
* Created by nilarnab on 7/8/16.
*/
public abstract class GenericRecyclerViewCursorAdapter<Vh extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<Vh> {
private Cursor mCursor;
private DataSetObserver mDataSetObserver;
public GenericRecyclerViewCursorAdapter(Cursor cursor) {
mCursor = cursor;
mDataSetObserver = new NotifyingDataSetObserver();
if (mCursor != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
setHasStableIds(true);
}
public Cursor getCursor() {
return mCursor;
}
@Override
public int getItemCount() {
return mCursor != null ? mCursor.getCount() : 0;
}
@Override
public long getItemId(int position) {
return position;
}
public abstract void onBindViewHolder(Vh viewHolder, Cursor cursor);
@Override
public void onBindViewHolder(Vh viewHolder, int position) {
if (mCursor == null || !mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(viewHolder, mCursor);
}
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
final Cursor oldCursor = mCursor;
if (oldCursor != null && mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (mCursor != null) {
if (mDataSetObserver != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
notifyDataSetChanged();
} else {
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
return oldCursor;
}
private class NotifyingDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
super.onChanged();
notifyDataSetChanged();
}
@Override
public void onInvalidated() {
super.onInvalidated();
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment