这段代码工作正常 - 我正在将一堆行加载到 SimpleCursorAdapter 中,ListView 会显示它们。好的。
SimpleCursorAdapter adapter;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.main_lv_projects);
lv.setOnItemClickListener(this);
String[] columns = new String[] { CompassDataProvider.ORG_NAME };
int[] names = new int[] { R.id.organisation_row_name};
cursor = this.managedQuery(CompassDataProvider.CONTENT_URI_ORGANISATIONS, null, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.organisation_row, cursor, columns, names);
lv.setAdapter(adapter);
startManagingCursor(cursor);
}
但是当我通过插入新行时
long rowID = db.getWritableDatabase().insert(CompassDBHelper.ORGS_TABLE_NAME, "", values);
getContext().getContentResolver().notifyChange(CONTENT_URI_ORGANISATIONS, null);
return Uri.withAppendedPath(CONTENT_URI_ORGANISATIONS, ""+rowID);
列表视图不会自行更新 - 我认为 SimpleCursorAdapter 会收到通知,然后可以重新加载它的视图 - 不是吗?
新行已创建 - 我检查了这个
当我使用
cursor.requery();
adapter.notifyDataSetChanged();
在我的 UI 威胁中,新数据被正确加载......我没有得到的观察者/听众模式是什么?:)
谢谢,马丁