2012-07-18 59 views
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?

回答

0

您可以在onCreate()方法调用

尝试

cursor.close() <-- 
helper.close(); 
+0

仍然有DatabaseObjectNotClosedException – darsh 2012-07-18 15:29:34

+0

我应该关闭在OnCreate其中光标。 Theres没有光标打开。 – darsh 2012-07-18 15:53:46