我有一个自定义的水平视图寻呼机来浏览列表视图,每个列表视图都是从数据库表中的条目中填充的。列表视图与数据库中的表一样多。现在的问题是我得到了 DatabaseObjectNotClosedException。我在 DbHelper 中有三个相关的方法。这是我的 DbHelper 中的代码。
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 等。我认为它没有用,因为当我浏览 listViews 时会显示 Execption。
我怎样才能摆脱这个 DatabaseObjectNotClosedException ?