ateiob
ateiob

Reputation: 9106

ContentResolver.insert() not reflected immediately?

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?

Upvotes: 4

Views: 1508

Answers (1)

vikki
vikki

Reputation: 2814

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.

Upvotes: 3

Related Questions