Created
May 5, 2015 16:18
-
-
Save jannisveerkamp/535a9e556c096f16a7ab to your computer and use it in GitHub Desktop.
Simple CursorAdapter for RecyclerView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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