views:

51

answers:

1

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

The adapter works so far, but I am getting so many errors on leaks and cursors that are not finalized. My question is: how do I close the 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 modifying 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 am getting the Invalid statement in fillWindow() error and the AutoCompleteTextView does not display the dropdown.

Here's how I set my adapter in my class, which by the way does not extend Activity but instead extends RelativeView (because I am using this to set the content 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);

I cannot use startManagingCursor(), so I close the cursor manually. But I still get the Cursor not finalized exception:

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)

Any ideas on how I can resolve these errors? Thanks!

A: 

First, you are closing too early. You cannot close the Cursor while it is being used by AutoCompleteCursorAdapter. I am also unsure if you can safely close the database while you have an open Cursor on it.

Second, I do not know why you say you "cannot use startManagingCursor()", because that would seem to be a fine answer in your case.

how do I close the db in runQueryOnBackgroundThread?

You could open the database in onCreate() and close it in onDestroy().

CommonsWare
Hi Mark! Maybe I am doing it wrong? I'm not sure if I understand how to approach this properly. My tabhost contains a view which contains the AutoCompleteTextView. I implemented this view in a separate class (and so it has no onCreate() and onDestroy()), and I only have a reference to the activity (i.e., the tabhost) through a parameter in the constructor. That's why I can't use startManagingCursor(). Or maybe there is a way, but I don't know how. (I'm not sure if I explained my predicament clearly.)
Zarah
@Zarah: IMHO, a custom `View` should not be querying a database -- that crushes what limited MVC/MVP structure we have in Android. Query your database from someplace else, such as the activity, and supply the results to the custom `View`, like you would with a `ListView` (`ListView` does not query a database -- you give it database results via a `CursorAdapter`). Regardless of where you query it, though, you can call `startManagingCursor()` on the `Activity`, which should be readily accessible from anywhere (e.g., `getContext()` in your custom `View`).
CommonsWare
Hi @Mark! Thanks for your input! Great insights, as always! :) Here's what I did. I queried the DB in my Activity, and passed on the cursor to the View. I am trying to use SimpleCursorAdapter instead of my custom adapter, to eliminate possible sources of error. However, the AutoCompleteTextView doesn't automatically filter the cursor's contents. From what I have read, I would have to create a FilterQueryProvider, which has an abstact runQuery() method. Does that mean I would need to requery (which brings us back to the MVC issue) or would you know of there another way?
Zarah
Hi again! I had a workaround. I created an interface to "force" the activity to implement a query, and then in the runQuery() method of the FilterQueryProvider, I just used the interface as such: mInterface.getRecentTerms(constraint.toString()). I will mark your answer as the correct answer because you pointed me in the correct direction. Thanks! :)
Zarah