1

嗨,我尝试将 SQLite 数据库中的数据加载到微调器中,因此在尝试访问已关闭的 CursorWindow 时出现此错误。我有这些类: CategoryBDD.java package com.example.mydrugstore;

public class CategoryBDD {

private SQLiteDatabase mdb;
private BasepharmaSQLiteHelper mabasepharma;

public CategoryBDD (Context context)
{
mabasepharma = new BasepharmaSQLiteHelper(context, Parambdd.DB_NAME, null,  Parambdd.SCHEMA_VERSION);
}

/** ouvre la base de données en écriture */
public void open(){
      //on ouvre la BDD en écriture
      mdb = mabasepharma.getWritableDatabase();
}

/** ferme la base de données */
public void close(){
      //on ferme l'accès à la BDD
      mdb.close();
}


public Category insertcategory( Category c)
{
 ContentValues valeur = new ContentValues();
    valeur.put(Parambdd.CATEGORY_NAME, c.getName());
    long insertId= mdb.insert(Parambdd.TABLECATEGORY, null, valeur);
    Cursor cursor = mdb.query(Parambdd.TABLECATEGORY,
            new String[] {Parambdd.CATEGORY_NAME}, Parambdd.CATEGORY_ID + " = " + insertId, null,
            null, null, null);
        cursor.moveToFirst();
        Category newcategory = cursorToCategory(cursor);
        cursor.close();
        return newcategory;
}

public int updateCategory(Category ctgr){
    ContentValues values = new ContentValues();
    values.put(Parambdd.CATEGORY_NAME, ctgr.getName());
    return mdb.update(Parambdd.TABLECATEGORY, values, Parambdd.CATEGORY_NAME + " = " +ctgr.getName(), null);
}


public void deletecategory ( Category c)
{
  long id = c.getId_category();
    mdb.delete(Parambdd.TABLECATEGORY,Parambdd.CATEGORY_ID+" = "+id, null);
}

public List<String> getAllCategorys() {
    List<String> categorys = new ArrayList<String>();
    Cursor cursor = mdb.query(Parambdd.TABLECATEGORY, new String[] {Parambdd.CATEGORY_NAME}, null, null, null, null, null);
    cursor.moveToFirst();
    if (cursorToCategory(cursor) == null)
    {
        return null;
    }
    else
    {
    while (!cursor.isAfterLast()) {
      Category c = cursorToCategory(cursor);
      categorys.add(c.getName());
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return categorys;
    }
  }

 private Category cursorToCategory(Cursor c){
     if (c.getCount() == 0)
           return null;
     c.moveToFirst();
     Category ctgr = new Category(c.getString(0));

 //    ctgr.setId_category(c.getLong(0));
   //  ctgr.setName(c.getString(1));

     c.close();
     return ctgr;
}
}

Main.java : 公共类 Main 扩展 Activity {

 Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adddrug);
    spinner = (Spinner) findViewById(R.id.spinner1);
    loadSpinnerData();


}


 private void loadSpinnerData() {
        // database handler
        CategoryBDD catdb = new CategoryBDD(getApplicationContext());
        catdb.open();

        Category c = new Category("toto");  //test insert category "toto"
        catdb.insertcategory(c);
     //   catdb.open();
        // Spinner Drop down elements
        List<String> categoryname = catdb.getAllCategorys();
        for (int i = 0;i < categoryname.size();i++)
        {
            Log.w("Main", categoryname.get(i));
        }

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, categoryname);

        // Drop down layout style - list view with radio button
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

谁能帮我 ?

4

3 回答 3

1

我通过在方法loadspinnerdata中删除“catdb.open()”并在这些方法“insertcategory和getallcategorys”中关闭对象sqldatabase来解决我的问题谢谢

于 2013-10-08T15:11:52.353 回答
0
// replace this code
private Category cursorToCategory(Cursor c){
     if (c.getCount() == 0)
           return null;
     c.moveToFirst();
     Category ctgr = new Category(c.getString(0));

 //    ctgr.setId_category(c.getLong(0));
   //  ctgr.setName(c.getString(1));

     return ctgr;
}
于 2013-10-05T11:56:21.153 回答
0

在 getAllCategorys() 方法中,您正在检查

if(cursorToCategory(cursor) == null){ }

在 cursorToCategory() 中,您正在关闭游标,并通过传递相同的游标再次调用相同的方法

Category c = cursorToCategory(cursor);

所以不要在 cursorToCategory() 方法中关闭光标。

于 2013-10-05T12:27:59.463 回答