Created
April 4, 2016 17:35
-
-
Save swolfand/e9814d654fc536641e6c64a4ff480941 to your computer and use it in GitHub Desktop.
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
package com.samwolfand.stockhawk.ui.adapter; | |
import android.app.Application; | |
import android.database.Cursor; | |
import android.database.DataSetObserver; | |
import android.support.v7.widget.RecyclerView; | |
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { | |
private static final String LOG_TAG = CursorRecyclerViewAdapter.class.getSimpleName(); | |
private Cursor mCursor; | |
private boolean dataIsValid; | |
private int rowIdColumn; | |
private DataSetObserver mDataSetObserver; | |
private Application mApplication; | |
public CursorRecyclerViewAdapter(Cursor cursor) { | |
mCursor = cursor; | |
dataIsValid = cursor != null; | |
rowIdColumn = dataIsValid ? mCursor.getColumnIndex("_id") : -1; | |
mDataSetObserver = new NotifyingDataSetObserver(); | |
if (dataIsValid) { | |
mCursor.registerDataSetObserver(mDataSetObserver); | |
} | |
} | |
public Cursor getCursor() { | |
return mCursor; | |
} | |
@Override | |
public int getItemCount() { | |
if (dataIsValid && mCursor != null) { | |
return mCursor.getCount(); | |
} | |
return 0; | |
} | |
@Override | |
public long getItemId(int position) { | |
if (dataIsValid && mCursor != null && mCursor.moveToPosition(position)) { | |
return mCursor.getLong(rowIdColumn); | |
} | |
return 0; | |
} | |
@Override | |
public void setHasStableIds(boolean hasStableIds) { | |
super.setHasStableIds(true); | |
} | |
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor); | |
@Override | |
public void onBindViewHolder(VH viewHolder, int position) { | |
if (!dataIsValid) { | |
throw new IllegalStateException("This should only be called when Cursor is valid"); | |
} | |
if (!mCursor.moveToPosition(position)) { | |
throw new IllegalStateException("Could not 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); | |
} | |
rowIdColumn = newCursor.getColumnIndexOrThrow("_id"); | |
dataIsValid = true; | |
notifyDataSetChanged(); | |
} else { | |
rowIdColumn = -1; | |
dataIsValid = false; | |
notifyDataSetChanged(); | |
} | |
return oldCursor; | |
} | |
private class NotifyingDataSetObserver extends DataSetObserver { | |
@Override | |
public void onChanged() { | |
super.onChanged(); | |
dataIsValid = true; | |
notifyDataSetChanged(); | |
} | |
@Override | |
public void onInvalidated() { | |
super.onInvalidated(); | |
dataIsValid = false; | |
notifyDataSetChanged(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment