0

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?

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Dale
  • 5,520
  • 4
  • 43
  • 79
  • The answers here: https://stackoverflow.com/questions/7973607/android-can-you-update-a-cursor-for-sqlite-results indicate cursors can not be updated, but content providers have that function. The problem is, content providers are supposed to be used *between* applications, not within them. – Dale Dec 01 '16 at 22:46

1 Answers1

0

Although the idea of running the complete query again seems inefficient, this example on github may be used to check the impact of re-querying a lot of records. In the onCreate() of the main list activity, you may put a large number in the repository.insertSome(n);. Set n to 40 or so for 1000 records, etc.

Now, when the user takes action on one of the records (a long-click), the database, model and view are updated, and a new cursor is created through a re-query. Although there are a lot of records in the database, the query does not seem to be using too many resources (i.e. it seems to respond quickly).

Dale
  • 5,520
  • 4
  • 43
  • 79