Crazy SQLite and autocomplete cursor leak

In my activity, I have an AutoCompleteTextView that gets its contents from my user adapter. I created my adapter by running this example .

The adapter still works, but I get so many errors on leaks and cursors that have not been finalized. My question is: how do I close db in runQueryOnBackgroundThread?

Here is my method:

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    if (getFilterQueryProvider() != null) { 
        return getFilterQueryProvider().runQuery(constraint); 
    }

    StringBuilder buffer = null;
    String[] args = null;

    if (constraint != null) {
        buffer = new StringBuilder();
        buffer.append("UPPER (");
        buffer.append(DbUtilities.KEY_SEARCH_TERM);
        buffer.append(") GLOB ?");
        args = new String[] { "*" + constraint.toString().toUpperCase() + "*" };
    }

    final DbUtilities favsDB = new DbUtilities(context);
    return favsDB.getAllRecents(buffer == null ? null : buffer.toString(), args);
}

I tried changing it to this:

final DbUtilities favsDB = new DbUtilities(context);
Cursor c = favsDB.getAllRecents(buffer == null ? null : buffer.toString(), args);
favsDB.close();
return c;

But I get an error Invalid statement in fillWindow(), and AutoCompleteTextView does not display a dropdown menu.

This is how I install my adapter in my class, which, by the way, does not extend the Activity, but extends the RelativeView (because I use this to set the contents of my tabs):

AutoCompleteTextView searchTerm = (AutoCompleteTextView) findViewById(R.id.autocomp_search_what);

DbUtilities db = new DbUtilities(mActivity.getBaseContext());
Cursor c = db.getAllSearchTerms();
AutoCompleteCursorAdapter adapter = new AutoCompleteCursorAdapter(mActivity.getApplicationContext(), 
    R.layout.list_item_autocomplete_terms, c);
c.close();
searchTerm.setAdapter(adapter);

startManagingCursor(), . Cursor, :

10-20 16:08:09.964: INFO/dalvikvm(23513): Uncaught exception thrown by finalizer (will be discarded):
10-20 16:08:09.974: INFO/dalvikvm(23513): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@43d63338 on search_terms that has not been deactivated or closed
10-20 16:08:09.974: INFO/dalvikvm(23513):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
10-20 16:08:09.974: INFO/dalvikvm(23513):     at dalvik.system.NativeStart.run(Native Method)

, ? !

+3
1

-, . Cursor, AutoCompleteCursorAdapter. , , Cursor.

-, , , startManagingCursor(), .

db runQueryOnBackgroundThread?

onCreate() onDestroy().

+3

Source: https://habr.com/ru/post/1770478/


All Articles