1

这段代码工作正常 - 我正在将一堆行加载到 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 威胁中,新数据被正确加载......我没有得到的观察者/听众模式是什么?:)

谢谢,马丁

4

1 回答 1

0

游标通过值而不是引用传递给 SimpleCursorAdapter。因此,调用 cursor.requery 无效。为了让它以你想要的方式工作,你真的需要实现一个 listAdapter 并在那里处理你的光标。

于 2011-06-26T12:38:56.037 回答