Skip to content

Instantly share code, notes, and snippets.

@jannisveerkamp
Created May 5, 2015 16:18
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 jannisveerkamp/535a9e556c096f16a7ab to your computer and use it in GitHub Desktop.
Save jannisveerkamp/535a9e556c096f16a7ab to your computer and use it in GitHub Desktop.
Simple CursorAdapter for RecyclerView
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
/**
* @author Jannis Veerkamp
* @since 05.05.15.
*/
public abstract class CursorAdapter<VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
Cursor mCursor;
public CursorAdapter(Cursor cursor) {
mCursor = cursor;
}
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
public Cursor getCursor() {
return mCursor;
}
@Override
public int getItemCount() {
return (mCursor == null) ? 0 : mCursor.getCount();
}
public Cursor swapCursor(Cursor newCursor) {
if (mCursor == newCursor) {
return null;
}
final Cursor oldCursor = mCursor;
mCursor = newCursor;
if (newCursor != null) {
notifyDataSetChanged();
}
return oldCursor;
}
private Object getItem(int position) {
if (mCursor != null) {
mCursor.moveToPosition(position);
}
return mCursor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment