1

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!

Alex Jenter
  • 4,324
  • 4
  • 36
  • 61
  • what adapter are you using and how? – pskink Jul 31 '15 at 11:18
  • I use a simple CursorAdapter-derived class that in its bindView method gets the data from the Cursor and assigns it to views from a ViewHolder (created previously in newView) – Alex Jenter Jul 31 '15 at 11:22
  • You can try out by loading data in chunks. – Beena Jul 31 '15 at 11:23
  • @Bini: Hm, and how do I do that with ContentProviders? They don't support LIMIT and OFFSET. And how does this fit with the loader framework? Is there anywhere an example of doing this? – Alex Jenter Jul 31 '15 at 11:25
  • first measure the time spent in your query – pskink Jul 31 '15 at 11:26
  • @pskink: I will need it in the future, but now I don't care for order. The query now is basically "SELECT _id, text, created FROM Notes". – Alex Jenter Jul 31 '15 at 11:26
  • and how fast is it? i mean just db query – pskink Jul 31 '15 at 11:27
  • The query is of course slow because it returns all rows. But I relied on the Cursor [data window and built-in lazy loading](http://stackoverflow.com/questions/31465069/does-android-sqlite-cursor-load-all-records-into-memory-at-once). – Alex Jenter Jul 31 '15 at 11:28
  • @pskink, you're the wizard! I just re-checked and indeed I had an order by an unindexed field! Please post your above comment as an answer and I'll accept it – Alex Jenter Jul 31 '15 at 11:40
  • place a breakpoint here: http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/database/sqlite/SQLiteCursor.java#122 and you will see how it is done – pskink Jul 31 '15 at 11:40

1 Answers1

3

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

pskink
  • 23,874
  • 6
  • 66
  • 77