0
我有一個自定義水平視圖分頁器來瀏覽列表視圖,每個列表視圖都是從數據庫表中的條目填充的。數據庫中會有與表一樣多的列表視圖。現在問題是我得到DatabaseObjectNotClosedException。我在DbHelper中有三個相關的方法。這是我的DbHelper中的代碼。DatabaseObjectNotClosedException in view pager
public int countTables() {
int count = 0;
String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
Cursor cursor =DB.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
count = cursor.getCount();
}
cursor.close();
count = count - 2;
return count;
}
public String getTableName(int count) {
String tabname = "";
String SQL_GET_ALL_TABLES = "SELECT * FROM SQLITE_SEQUENCE";
Cursor cursor = DB.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToPosition(count);
tabname = cursor.getString(cursor.getColumnIndex("name"));
cursor.close();
DB.close();
return tabname;
}
public Cursor getAll(String tableName) {
return (getReadableDatabase().rawQuery("SELECT _id, note type FROM "
+ tableName, null));
}
這是我的主要活動,包含視圖尋呼機類。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewp);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);
myPager.setAdapter(adapter);
DbHelper helper = new DbHelper(this);
count = helper.countTables();
myPager.setCurrentItem(count - 1);
temp1 = temp = helper.countTables() - 1;
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return count;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
helper = new DbHelper(Listing.this);
View view = null;
if (position == count) {
view = inflater.inflate(R.layout.middle, null);
ListView list = (ListView) view.findViewById(R.id.listView1);
// helper = new DbHelper(Listing.this);
String tabName = helper.getTableName(position);
dataset_cursor = helper.getAll(tabName);
startManagingCursor(dataset_cursor);
adapter5 = new NoteAdapter(dataset_cursor);
list.setAdapter(adapter5);
// adapter.notifyDataSetChanged();
temp--;
temp1--;
}
if (position < temp) {
temp--;
temp1--;
view = inflater.inflate(R.layout.left, null);
ListView list1 = (ListView) view.findViewById(R.id.listView1);
// helper = new DbHelper(Listing.this);
String tabName = helper.getTableName(position);
dataset_cursor1 = helper.getAll(tabName);
startManagingCursor(dataset_cursor1);
adapter1 = new NoteAdapter(dataset_cursor1);
list1.setAdapter(adapter1);
// adapter1.notifyDataSetChanged();
// Toast.makeText(getBaseContext(), tabName, Toast.LENGTH_LONG);
temp--;
temp1--;
}
if (position > temp) {
temp++;
temp1++;
view = inflater.inflate(R.layout.right, null);
ListView list2 = (ListView) view.findViewById(R.id.listView1);
// helper = new DbHelper(Listing.this);
String tabName = helper.getTableName(position);
dataset_cursor2 = helper.getAll(tabName);
startManagingCursor(dataset_cursor2);
adapter2 = new NoteAdapter(dataset_cursor2);
list2.setAdapter(adapter2);
// adapter2.notifyDataSetChanged();
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
countTable()首先被執行,getTableName()是第二個。 我不能在方法countTables中使用DB.close(),因爲當我使用getTableName獲取表名時,活動強制關閉,表示數據庫已關閉。
我試着覆蓋onDestroy,onStop等我認爲它沒有用,因爲Execption顯示當我通過列表視圖。
我該如何擺脫這個DatabaseObjectNotClosedException?
仍然有DatabaseObjectNotClosedException – darsh 2012-07-18 15:29:34
我應該關閉在OnCreate其中光標。 Theres沒有光標打開。 – darsh 2012-07-18 15:53:46