gpt4 book ai didi

android - 错误 : Attempting to access a closed cursor window

转载 作者:行者123 更新时间:2023-11-30 03:20:42 28 4
gpt4 key购买 nike

您好,我尝试将数据从 SQLite 数据库加载到微调器中,所以我在尝试访问已关闭的 CursorWindow 时遇到此错误。我有这些类(class):分类BDD.java 包 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;
}
}

主要.java : 公共(public)类 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;
}
}

谁能帮帮我?

最佳答案

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

关于android - 错误 : Attempting to access a closed cursor window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197499/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com