Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 397
ateiob Avatar asked Oct 27 '25 20:10

ateiob


1 Answers

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.

like image 126
vikki Avatar answered Oct 29 '25 12:10

vikki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!