我有一個onCreate()
方法,它使用datasource.open()
和datasource.close()
。如果在onCreate()
的末尾發出datasource.close();
,我的ListView SimpleCursorAdapter始終爲空。關閉數據庫和光標與ListView
如果我從onCreate()
的末尾刪除datasource.close();
,則會填充ListView。
我無法弄清楚爲什麼這是我的生活。
任何人都有任何文件說明爲什麼發生這種情況?
示例代碼:
@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();
}
爲什麼使用Content Provider?由此可以使用光標。 – 2012-07-29 05:52:53
這取決於你的光標對象的範圍。發佈我們的代碼2得到正確的理由.. – Jayabal 2012-07-29 06:41:16
添加代碼。這是正常的通用listview適配器連接使用簡單的遊標適配器 – 2012-07-29 15:49:31