Makeable wrote:
> I must have searched high and low for the answer to my problem - I've
> even spent around $100 on books hoping that one of them would cover it
> - alas it was not the case.

Since I think I represented a third or so of that dollar total, I
apologize for not covering your scenario. Perhaps in some future update
to my books I'll tackle it.

> I have approximately 50000 records in a database table. All I want to
> do is display them in a list-view so you can scroll through them.

What do your *users* want?

If I were one of your users, forcing me to scroll through 50,000 rows in
a list would result in a quick trip to the uninstall button. That's why
mankind invented categories, and drill-downs, and searching, and other
means to present smaller lists at a time. I'd get annoyed at 50,000 rows
in a list on a *desktop* app, let alone a mobile one.

But, hey, that's just me.

Not to mention that a query returning 50,000 items is going to create a
ton of garbage, since Cursors hold whole result sets. You're going to
have some very expensive garbage collection runs after you are done with
the query results, and that may not only impact your application but
others (e.g., games that can't run at full speed due to all the garbage
collection). Finding a way to support 50,000 items and possible big list
flings while not consuming tons of RAM and creating gobs of garbage will
be tricky.

> I dare say I will need to lazily load them as you scroll through the
> list - my question is how and can it be done through a content
> provider?
> 
> I noticed the SlowAdapter in the APIDemos that appears to do exactly
> the kind of thing that I am looking for, but Im unsure of the correct
> method for requerying the content provider with the new limit criteria
> (or at all via a ContentProvider). Do I need to create a new cursor
> and merge it with the previous one as I scroll down?

ContentProvider does not have a notion of limit criteria, outside of
what you might somehow encode in the WHERE clause of your query(). And
Cursors are not designed to morph their queries to change limit clauses.

Here are a few options:

1. Skip the ContentProvider, directly call the SQLite database using a
limit clause, but with your own CursorFactory where you return your own
SQLiteCursor subclass that uses respond() to coordinate limits with the
ListView. When the ListView is scrolled near the bottom, you tell your
custom Cursor to expand its limit range. And, you hope this all doesn't
take too damn long.

2. Clone or subclass MergeCursor to allow you to add new Cursors to its
collection on the fly. Have your ContentProvider somehow hack in a limit
constraint into its API (e.g., if you aren't using ORDER BY, put the
limit clause there). When your ListView is scrolled near the bottom, run
a fresh query for the next set of rows, and add the resulting cursor to
the MergeCursor. You may need to invalidate() the ListView to get it to
recognize the newly available rows.

3. Same as #2, but try a MatrixCursor or CursorWrapper instead.

4. Roll your own Cursor class that deals with your targeted set of
results. In particular, it would report 50,000 for getCount(), but
internally, it would run *its own queries* of the ContentProvider as
needed based on moveNext() and moveToPosition() to have a window of data
around the targeted row. You could then have your own caching algorithms
so you would not need 50,000 records' worth of data in RAM if the user
does a fling over the whole list. You'll see examples of creating custom
Cursors using CursorWrapper and such in the Advanced Android book.

5. Just don't put 50,000 items in a list.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to