我 Activity 的 onCreate()
中的以下代码 fragment 在后续调用中效果很好(即 cursor.moveToFirst()
为 true)我的申请:
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?");
所以,我知道肯定 cr.insert()
已正确执行。
但是在第一次调用我的应用程序时,cursor.moveToFirst()
返回false,尽管 cr.insert()
在它之前。
为什么?
我需要某种“刷新”或“关闭”语句吗?
最佳答案
插入后需要获取一个新游标,因为游标本身不监听数据库中的变化。
或者写一个Content Observer和 inform它的变化。
关于android - ContentResolver.insert() 没有立即反射(reflect)出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11848871/