2

The following code snippet from my activity's onCreate() works great (i.e. cursor.moveToFirst() is true) in subsequent invocations of my application:

ContentValues values = new ContentValues();
values.put(MyProvider.Notes.TITLE, "title");
values.put(MyProvider.Notes.NOTE, "note");
ContentResolver cr = getContentResolver();
Uri newlyInsertedRecord = cr.insert(notesUri, values);

if (cursor.moveToFirst()) {
    int id = cursor.getInt(0);
    String ttl = cursor.getString(1);
    String nte = cursor.getString(2);
    Log.i(TAG, id + ", "+ ttl + ", "+ nte);
}
else
    Log.e(TAG, "Why isn't the insert reflected immediately?");

So, I know for certain that the cr.insert() is executed properly.

But on first invocation of my application, cursor.moveToFirst() returns false, despite the cr.insert() preceding it.

Why?

Do I need some sort of a "flush" or "close" statement?

ateiob
  • 9,016
  • 10
  • 44
  • 55

1 Answers1

2

You need to acquire a new cursor after doing the insert, as the cursor in itself doesn't listen for changes in the db.

Or write a Content Observer and inform it of the changes.

Community
  • 1
  • 1
vikki
  • 2,766
  • 1
  • 20
  • 26