Search code examples
androidandroid-listviewandroid-contentprovider

Heavy ContentProvider query and ListView


Sorry for a probably noob question, but couldn't find the answer despite studying numerous tutorials and other questions here on SO.

What I want to do is fairly simple: display an Android ListView with a lot of database-stored strings. And by "a lot" I mean up to 100MB, but the ListView should start displaying things without delay.

The context is: this is the note management app, and by default it should display all user's notes unfiltered. I'm studying how the app will behave if there are a lot of notes.

Now I use the recommended approach with the Loader framework: kicking off a ContentProvider query in onCreateLoader, and assigning the resulting Cursor to the ListView's adapter in onLoadFinished.

Still this is painfully slow (initial load up to 10s). I've studied the sources of Cursor and know that it keeps a "window" of data, so that not all 100MB of text should be loaded at once. However it seems that this is the case.

I thought this to be a fairly common task, and am surprised not to find any good information on it. This is the critical part of the app, and I want to get it right from the start - it will be difficult to refactor this later.

So could anyone please help with this? Is lazy loading the answer? Why doesn't Cursor do it itself? How to do it properly - do I have to cache an array of IDs first and load chunks upon scrolling?

Thanks!


Solution

  • SimpleCursorAdapter + CursorLoader should work just fine, seems that your db query is that slow, most likely you want to sort the data on unindexed field