This question is about an efficient and appropriate way to manage small changes to an SQLiteDatabase. For disucssion purposes, we can look at this code, which I've summarized here:
CursorRecyclerViewAdapter.java
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
public CursorRecyclerViewAdapter(Context context, Cursor cursor) {
public Cursor getCursor() {
public int getItemCount() {
@Override public long getItemId(int position) {
@Override public void setHasStableIds(boolean hasStableIds) {
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor);
@Override public void onBindViewHolder(VH viewHolder, int position) {
public void changeCursor(Cursor cursor) {
public Cursor swapCursor(Cursor newCursor) {
private class NotifyingDataSetObserver extends DataSetObserver {
@Override public void onChanged() {
@Override public void onInvalidated() {
MyListCursorAdapter.java
public class MyListCursorAdapter extends CursorRecyclerViewAdapter<MyListCursorAdapter.ViewHolder>{
public MyListCursorAdapter(Context context,Cursor cursor){
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@Override public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
The scenario is as follows: the user of the application takes some action which needs to flip a bit on one of the records in the database. In the listener for that action, it's easy to change the one record in the database, and make sure the view is updated with the change. But my cursor is no longer in sync. The way I've "solved" the problem is to re-query the database and swap-in the resulting new cursor. But that seems like it's killing a fly with a hammer. I was hoping for some magic from the RecyclerView.notifyDataSetChanged()
method, but it's not doing anything.
So, given a Cursor
loaded with data and a small change, is it efficient and appropriate to run the query again, get a new Cursor
and swap it, or is there a better way?