-1

我有一个onCreate()使用datasource.open()and的方法datasource.close()。如果datasource.close();在末尾发出问题onCreate(),我的 ListView SimpleCursorAdapter 始终为空白。

如果我datasource.close();从 ListView 的末尾删除,onCreate()则会填充。

我一生都无法弄清楚为什么会这样。

任何人都有任何文件说明为什么会发生这种情况?

示例代码:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    Bundle extras = getIntent().getExtras();
    id = extras.getLong("extraID");

    setContentView(R.layout.main);

    datasource = new SMSDataSource(this);
    datasource.open();

    Cursor groupCursor = datasource.queryByGroup(long id);

    contactAdapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_2, // Use a template
            // that displays a
            // text view
            getCur, // Give the cursor to the list adapter
            new String[] { DBManagement.CONTACTS_COLUMN_NAME,
            DBManagement.CONTACTS_COLUMN_NUMBER }, // Map the NAME
            // column in the
            // people database to...
            layouts); // The "text1" view defined in
    // the XML template

    list = (ListView) findViewById(android.R.id.list);
    list.setAdapter(contactAdapter);

    registerForContextMenu(list);

    groupCursor.close();
    datasource.close();
}
4

2 回答 2

1

如果你想手动管理你的光标,你应该调用deactiveate()你的活动onStop然后requery()继续。

但是,我会简单地使用Activity.startManagingCursor()which 会为您解决这个问题。此方法被标记为已弃用,因此如果您想完全按照 Google 向您推荐的方式执行操作,您可能希望使用CursorLoader类来异步处理光标。坦率地说,除非您的数据库非常大和/或您的查询运行速度非常慢,否则使用不推荐使用的方法没有害处,它暂时不会有任何影响。

于 2012-07-29T16:20:27.717 回答
0

实际上我认为datasource.close()方法调用应该放在onPause(),onStop()onDestroy方法中的某个地方..但没有必要如果您发布一些代码片段,我可以提供更准确的答案..

于 2012-07-29T07:29:06.207 回答