I have implemented an app that has activity which uses a searchview in its actionbar.

The thing is that while developing and testing on modern devices (android 4.4) I never had any problem, but now that I have tested the app in some devices with android 3.0 or android 4.0 the app sometimes crashes while searching and gives me this exception:

E/AndroidRuntime(457): Caused by: java.lang.IllegalStateException: database /data/data/com.ss.quicorder/databases/QuickOrder.db (conn# 0) already closed
11-25 11:44:37.864: E/AndroidRuntime(457):  at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2117)

This is the code I use to search:

searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextChange(String query) {
                if (!query.isEmpty() && query.trim().length()>0) { 
                    displayResults(query);
                }
                else{                   
                    mResultsListView.setAdapter(null);
                }
                return false;

            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

        });

The displayResults function only executes an AsyncTask:

    private void displayResults(String query) {
        new sSearchCustomers().execute(query);
    }

The AsyncTask the queries the database:

class SearchCustomers extends AsyncTask<String, Void, Cursor> {

        @Override
        protected Cursor doInBackground(String... params) {
            // get the query
            String stringToDisplay = params[0].toLowerCase(Locale.getDefault());
            String stringToSearch = Normalizer.normalize(stringToDisplay,
                    Normalizer.Form.NFC);
            Cursor resultados = SearchCustomersActivity.this.mDB
                    .searchCustomersByName((stringToSearch != null ? stringToSearch
                            : "@@@@"));
            return resultados;

        }

        @Override
        protected void onPostExecute(Cursor result) {           
            if (result != null) {               
                String[] from = new String[] {  
                        QuickOrderDB.CUSTOMER_NAME,
                        QuickOrderDB.CUSTOMER_ADDRESS };

                int[] to = new int[] { 
                        R.id.nameTextView,
                        R.id.addressTextView};

                SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(
                        SearchCustomersActivity.this,
                        R.layout.customer_results_item, result, from, to,0);
                mResultsListView.setAdapter(cursorAdapter);             

            }
        }

    }

And this the code for searchCustomersByName :

   public Cursor searchCustomersByName (String name {
        String where = CUSTOMER_NAME_NORM + " LIKE ? ";
        String[] whereArgs = { "%" + descripcion + "%"};

        this.openReadableDB();
        Cursor cursor = db.query(CUSTOMER_TABLE, null, where, whereArgs, null,
                null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        this.closeDB();
        return cursor;

    }

As you can see in the code for searchCustomersByName , every time I return a Cursor I close the database, but I'm not sure if executing an AsyncTask for every single letter the user types in is what it's causing this, I mean, is there any chance that one AsyncTask closes the database while another is performing a query. Aren't they supposed to be executed sequentially?? Also the fact of never being able to close the cursor really worries me.

As always, any ideas or suggestions would be greatly appreciated.

Thanks.

0

There are 0 best solutions below