2013-10-05 51 views
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: 公共類主要擴展活動{

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; 
} 
} 

誰能幫助我?

回答

1

我通過刪除「catdb.open()」中的方法loadspinnerdata和在這些方法中「insertcategory和getallcategorys」關閉對象sqldatabase 感謝

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; 
} 
+0

什麼斗的意思是將這段代碼 – medman

+0

PLZ比較這代碼解決我的問題與ur代碼,看看差異.. –

+0

對不起,但我已經做到了,但總是相同的錯誤 – medman

0

在getAllCategorys()方法正在檢查的

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

()你正在關閉光標並再次您正在通過使相同的光標

Category c = cursorToCategory(cursor); 
調用相同的方法

所以不要關閉cursorToCategory()方法中的遊標。

+0

我已經刪除cursorToCategory中的c.close(),但我有同樣的錯誤 – medman