Android的SQLite的:尝试重新打开已关闭的对象对象、Android、SQLite

由网友(渡)分享简介:我试图根据国际标准书号从我的库存表中获取一定的图书数据。不过,我得到一个错误:尝试重新打开已关闭的对象。当我点击一个ListView对象,去一个不同的屏幕,返回到该页面通过完成(),然后尝试点击另一个ListView控件对象只出现此错误。我搬到了字符串searchEntries [] = InventoryAdapte...

我试图根据国际标准书号从我的库存表中获取一定的图书数据。不过,我得到一个错误:尝试重新打开已关闭的对象。当我点击一个ListView对象,去一个不同的屏幕,返回到该页面通过完成(),然后尝试点击另一个ListView控件对象只出现此错误。我搬到了字符串searchEntries [] = InventoryAdapter.getInventoryEntriesByISBN(SEARCHQUERY,ISBN [位置]); onClickListener 来在previous的为前 onClickListener 循环的,现在它的作品。

为什么,如果我尝试通过getInventoryEntriesByISBN完成(),从另一个活动返回到本次活动后,它不工作?

在SearchResultsScreen发生错误:

 字符串searchEntries [] = InventoryAdapter.getInventoryEntriesByISBN(SEARCHQUERY,ISBN [位置]); 

和推而广之,发生在InventoryAdapter:

 光标光标= db.rawQuery(查询,新的String [] {ISBN}); 

SearchResultsScreen.java

  //设置搜索阵列    的for(int i = 0; I< isbn.length;我++)    {        sea​​rchArray.add(新InventoryItem(ISBN [I],InventoryAdapter.getTitleAndAuthorByISBN(ISBN由[i])));    }    Toast.makeText(getApplicationContext(),searchArray.size()=+ searchArray.size(),Toast.LENGTH_LONG).show();    //添加自定义适配器数据    适配器=新CustomAdapter(这一点,R.layout.list,searchArray);    DataList控件的ListView =(ListView控件)findViewById(R.id.list);    dataList.setAdapter(适配器);    //在点击============================================== ==========    dataList.setOnItemClickListener(新OnItemClickListener(){        @覆盖        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,                长ID){            字符串searchEntries [] = InventoryAdapter.getInventoryEntriesByISBN(SEARCHQUERY,ISBN [位置]); 
Android 安卓的sqlite表删除后,无法新建

InventoryAdapter.java(最相关的部分)

 公共字符串[] getInventoryEntriesByISBN(字符串搜索,字符串ISBN){    的String [] = searchEntry新的String [9];    //查询    查询字符串=从清单中选择*其中ISBN =?;    光标光标= db.rawQuery(查询,新的String [] {ISBN});    如果(cursor.getCount()。1)//标题不存在    {        cursor.close();        的for(int i = 0; I< 9;我++)            sea​​rchEntry [I] =未找到;        返回searchEntry;    }    cursor.moveToFirst();    //将数据放入相应的变量    INT发布= cursor.getInt(cursor.getColumnIndex(PUBLISH_DATE));    字符串publishdate =((整数)发布)的ToString();    字符串title = cursor.getString(cursor.getColumnIndex(TITLE));    笔者字符串= cursor.getString(cursor.getColumnIndex(作者));    串callNumber = cursor.getString(cursor.getColumnIndex(CALL_NUMBER));    INT可用= cursor.getInt(cursor.getColumnIndex(AVAILABLE_COUNT));    字符串availablecount =((整数)可用)的ToString();    INT库存= cursor.getInt(cursor.getColumnIndex(INVENTORY_COUNT));    字符串inventorycount =((整数)存货)的ToString();    INT由于= cursor.getInt(cursor.getColumnIndex(DUE_PERIOD));    字符串dueperiod =((整数)因)的ToString();    INT checkoutcount = cursor.getInt(cursor.getColumnIndex(COUNT));    字符串数=((整数)checkoutcount)的ToString();    //合并变量放入一个阵列    sea​​rchEntry [0] = ISBN;    sea​​rchEntry [1] =称号;    sea​​rchEntry [2] =作者;    sea​​rchEntry [3] = publishdate;    sea​​rchEntry [4] = callNumber;    sea​​rchEntry [5] = availablecount;    sea​​rchEntry [6] = inventorycount;    sea​​rchEntry [7] = dueperiod;    sea​​rchEntry [8] =计数;    cursor.close();    返回searchEntry;}公共字符串getTitleAndAuthorByISBN(字符串ISBN)    {        INT entriesFound = getNumSearchEntries(ISBN);        如果(entriesFound == 0)            entriesFound = 1;        串searchEntry;        //查询        查询字符串=从清单中选择*其中ISBN =?;        光标光标= db.rawQuery(查询,新的String [] {ISBN});        如果(cursor.getCount()。1)//标题不存在        {            cursor.close();            sea​​rchEntry =未找到;            返回searchEntry;        }        cursor.moveToFirst();        //将数据放入相应的变量        字符串title = cursor.getString(cursor.getColumnIndex(TITLE));        笔者字符串= cursor.getString(cursor.getColumnIndex(作者));        //变量结合成一个字符串        sea​​rchEntry =标题+/+作者;        //关闭游标及返回        cursor.close();        返回searchEntry;    } 

DataBaseHelper.java

 公共类DataBaseHelper扩展SQLiteOpenHelper{//数据库版本    私有静态最终诠释DATABASE_VERSION = 1;//数据库名称私有静态最后弦乐DATABASE_NAME =database.db;// ============================结束变量================== =========公共DataBaseHelper(上下文的背景下,字符串名称,CursorFactory工厂,INT版){           超(背景下,名称,厂家,版本);}公共DataBaseHelper(上下文的背景下){    超(背景下,DATABASE_NAME,空,DATABASE_VERSION);}//当盘和辅助类的需求不存在的数据库调用//创建一个新的。@覆盖公共无效的onCreate(SQLiteDatabase _db){        _db.execSQL(LoginDataBaseAdapter.USER_TABLE_CREATE);        _db.execSQL(CheckOutDataBaseAdapter.CHECKOUT_TABLE_CREATE);        _db.execSQL(InventoryAdapter.INVENTORY_TABLE_CREATE);        _db.execSQL(StatisticsAdapter.STATISTICS_TABLE_CREATE);}//当有一个数据库版本不匹配意味着该版本,称为//在磁盘上的数据库需要升级到最新版本。@覆盖公共无效onUpgrade(SQLiteDatabase _db,诠​​释_oldVersion,诠释_newVersion){        //日志的版本升级。        Log.w(TaskDBAdapter,从版本升级+ _oldVersion +到+ _newVersion +,这将摧毁所有旧数据);        //升级现有的数据库,以符合新的版本。多        // previous版本可以通过比较_oldVersion和_newVersion处理        //值。        //在升级旧的掉落表        _db.execSQL(DROP TABLE IF EXISTS+ LoginDataBaseAdapter.USER_TABLE_CREATE);        _db.execSQL(DROP TABLE IF EXISTS+ CheckOutDataBaseAdapter.CHECKOUT_TABLE_CREATE);        _db.execSQL(DROP TABLE IF EXISTS+ InventoryAdapter.INVENTORY_TABLE_CREATE);        _db.execSQL(DROP TABLE IF EXISTS+ StatisticsAdapter.STATISTICS_TABLE_CREATE);        //创建一个新的。        的onCreate(_db);}} 

解决方案

当我点击一个项目,去一个不同的屏幕,通过回到此页完成(),然后尝试单击只出现错误在另一个的ListView对象。

我感动字符串searchEntries [] = InventoryAdapter.getInventoryEntriesByISBN(SEARCHQUERY,ISBN [位置]);从onClickListener到previous为onClickListener前环和现在的作品。

正确SearchResultsScreen是如下:

SearchResultsScreen.java

  //设置搜索阵列    最终字符串项[] [] =新的String [isbn.length] [9];    的for(int i = 0; I< isbn.length;我++)    {        sea​​rchArray.add(新InventoryItem(ISBN [I],InventoryAdapter.getTitleAndAuthorByISBN(ISBN由[i])));        参赛作品由[i] = InventoryAdapter.getInventoryEntriesByISBN(SEARCHQUERY,ISBN [I]);    }    Toast.makeText(getApplicationContext(),searchArray.size()=+ searchArray.size(),Toast.LENGTH_LONG).show();    //添加自定义适配器数据    适配器=新CustomAdapter(这一点,R.layout.list,searchArray);    DataList控件的ListView =(ListView控件)findViewById(R.id.list);    dataList.setAdapter(适配器);    //在点击============================================== ==========    dataList.setOnItemClickListener(新OnItemClickListener(){        @覆盖        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,                长ID){            字符串searchEntries [] =条目[位置] 

I'm trying to get certain book data from my Inventory table based on the ISBN. However, I'm getting an error: "attempt to re-open an already-closed object". The error only occurs when I click a listView object, go to a different screen, go back to this page via "finish()", and then try to click on another listView object. I moved the String searchEntries[] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[position]); from the onClickListener to the previous for loop before the onClickListener and now it works.

Why does it not work if I try to getInventoryEntriesByISBN after returning to this activity from another activity via "finish()"?

The error occurs at SearchResultsScreen:

String searchEntries[] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[position]);

and by extension, occurs at InventoryAdapter:

Cursor cursor = db.rawQuery(query, new String[] {ISBN});

SearchResultsScreen.java

// Set up search array
    for(int i = 0; i < isbn.length; i++)
    {
        searchArray.add(new InventoryItem(isbn[i], InventoryAdapter.getTitleAndAuthorByISBN(isbn[i])));
    }
    Toast.makeText(getApplicationContext(), "searchArray.size()="+searchArray.size(), Toast.LENGTH_LONG).show();

    // add data in custom adapter
    adapter = new CustomAdapter(this, R.layout.list, searchArray);
    ListView dataList = (ListView) findViewById(R.id.list);
    dataList.setAdapter(adapter);

    // On Click ========================================================
    dataList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            String searchEntries[] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[position]);

InventoryAdapter.java (Most relevant parts)

public String[] getInventoryEntriesByISBN(String search, String ISBN)
{
    String[] searchEntry = new String [9];
    //Query
    String query = "select * from INVENTORY where ISBN = ?";
    Cursor cursor = db.rawQuery(query, new String[] {ISBN});
    if(cursor.getCount()<1) // title Not Exist
    {
        cursor.close();
        for(int i = 0; i < 9; i++)
            searchEntry[i] = "Not Found";
        return searchEntry;
    }
    cursor.moveToFirst();

    //put data into respective variable
    int publish = cursor.getInt(cursor.getColumnIndex("PUBLISH_DATE"));
    String publishdate = ((Integer)publish).toString();
    String title = cursor.getString(cursor.getColumnIndex("TITLE"));
    String author = cursor.getString(cursor.getColumnIndex("AUTHOR"));
    String callNumber = cursor.getString(cursor.getColumnIndex("CALL_NUMBER"));
    int available = cursor.getInt(cursor.getColumnIndex("AVAILABLE_COUNT"));
    String availablecount = ((Integer)available).toString();
    int inventory = cursor.getInt(cursor.getColumnIndex("INVENTORY_COUNT"));
    String inventorycount = ((Integer)inventory).toString();
    int due = cursor.getInt(cursor.getColumnIndex("DUE_PERIOD"));
    String dueperiod = ((Integer)due).toString();
    int checkoutcount = cursor.getInt(cursor.getColumnIndex("COUNT"));
    String count = ((Integer)checkoutcount).toString();
    //combine variables into one array
    searchEntry[0] = ISBN;
    searchEntry[1] = title;
    searchEntry[2] = author;
    searchEntry[3] = publishdate;
    searchEntry[4] = callNumber;
    searchEntry[5] = availablecount;
    searchEntry[6] = inventorycount;
    searchEntry[7] = dueperiod;
    searchEntry[8] = count;

    cursor.close();
    return searchEntry;
}

public String getTitleAndAuthorByISBN(String ISBN)
    {
        int entriesFound = getNumSearchEntries(ISBN);
        if(entriesFound==0)
            entriesFound = 1;
        String searchEntry;
        //Query
        String query = "select * from INVENTORY where ISBN = ?";
        Cursor cursor = db.rawQuery(query, new String[] {ISBN});
        if(cursor.getCount()<1) // title Not Exist
        {
            cursor.close();
            searchEntry = "Not Found";
            return searchEntry;
        }
        cursor.moveToFirst();
        //put data into respective variable
        String title = cursor.getString(cursor.getColumnIndex("TITLE"));
        String author = cursor.getString(cursor.getColumnIndex("AUTHOR"));
        //combine variables into one String
        searchEntry = title + " / " + author;
        //close cursor and return
        cursor.close();
        return searchEntry;
    }

DataBaseHelper.java

public class DataBaseHelper extends SQLiteOpenHelper
{   
// Database Version
    private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "database.db";

// ============================ End Variables ===========================

public DataBaseHelper(Context context, String name, CursorFactory factory, int version) 
{
           super(context, name, factory, version);
}

public DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db) 
{
        _db.execSQL(LoginDataBaseAdapter.USER_TABLE_CREATE);
        _db.execSQL(CheckOutDataBaseAdapter.CHECKOUT_TABLE_CREATE);
        _db.execSQL(InventoryAdapter.INVENTORY_TABLE_CREATE);
        _db.execSQL(StatisticsAdapter.STATISTICS_TABLE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
{
        // Log the version upgrade.
        Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");


        // Upgrade the existing database to conform to the new version. Multiple
        // previous versions can be handled by comparing _oldVersion and _newVersion
        // values.
        // on upgrade drop older tables
        _db.execSQL("DROP TABLE IF EXISTS " + LoginDataBaseAdapter.USER_TABLE_CREATE);
        _db.execSQL("DROP TABLE IF EXISTS " + CheckOutDataBaseAdapter.CHECKOUT_TABLE_CREATE);
        _db.execSQL("DROP TABLE IF EXISTS " + InventoryAdapter.INVENTORY_TABLE_CREATE);
        _db.execSQL("DROP TABLE IF EXISTS " + StatisticsAdapter.STATISTICS_TABLE_CREATE);

        // Create a new one.
        onCreate(_db);
}

}

解决方案

The error only occurs when I click an item, go to a different screen, go back to this page via "finish()", and then try to click on another listView object.

I moved the String searchEntries[] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[position]); from the onClickListener to the previous for loop before the onClickListener and now it works.

The correct SearchResultsScreen is below:

SearchResultsScreen.java

// Set up search array
    final String Entries[][] = new String[isbn.length][9];
    for(int i = 0; i < isbn.length; i++)
    {
        searchArray.add(new InventoryItem(isbn[i], InventoryAdapter.getTitleAndAuthorByISBN(isbn[i])));
        Entries[i] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[i]);
    }
    Toast.makeText(getApplicationContext(), "searchArray.size()="+searchArray.size(), Toast.LENGTH_LONG).show();

    // add data in custom adapter
    adapter = new CustomAdapter(this, R.layout.list, searchArray);
    ListView dataList = (ListView) findViewById(R.id.list);
    dataList.setAdapter(adapter);

    // On Click ========================================================
    dataList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            String searchEntries[] = Entries[position];

阅读全文

相关推荐

最新文章