Grokking Android

Getting Down to the Nitty Gritty of Android Development

Android: How to Use Two Data Sources in ListViews?

By 3 Comments

Sometimes in our apps we have the need to link to data of other sources. You have a database of your own for your app, but some records also point to the MediaStore content provider to calendar entries or to contacts.

For all detail pages this is not much of a problem. You get the data from your database and query the external content provider for additional data based on the id or lookup uri that you stored in your database.

But what to do, if you want to show data in ListViews? Say contact pictures, event titles for multiline list elements and so on. Then it gets complicated.

From what I see, we have the following choices. Neither is fully satisfying – which is why I would like to hear how you deal with these kind of problems.

Now I would really like to hear what you think. Probably there are other options, I have overlooked. Or better ways to make one of these three options work. Please let me know in the comments, on G+ or on Twitter. Thanks!

If you are curious about the G+ discussion, have a look at my G+ announcement of this post.

Wolfram Rittmeyer lives in Germany and has been developing with Java for many years.

He recently shifted his attention to Android and blogs about anything interesting that came up while developing for Android.

You can find him on Google+ and Twitter.

3 thoughts on “Android: How to Use Two Data Sources in ListViews?”

  1. I vote for solution number #1 !!!

    More seriously, I consider this problem is exactly similar to the having an image coming from the network in your ListView’s items. Even if contacts and/or media entries are on the same device, sandboxing makes you see these data as if they were available on a server.

    As far as I know, there are actually no way to “join” ContentProviders data. In my opinion, this is due to Android sandboxing and also to the fact a ContentProvider doesn’t know the underlying persistance mechanism (in-memory, SQLite database, etc.)

    If we were sure that a ContentProvider was SQlite-based and there was no sandboxing we could have imagined a way to open the two underlying databases and query them. That would make everything faster, of course, but that would also be a huge break to the “encapsulation principle”.

  2. I also had this problem of inter-classing two cursors and displaying them in a ListView.
    My solution was to wrap the original cursors in a custom MergeCursor that extends AbstractCursor. In the onMove method I looked for the row that comes next in each cursor. (There’s a built-in MergeCursor but that was not doing what I needed).

  3. I’d say mostly #1, though if all the data is in my own database, i join the data from multiple tables and use the cursor. Yup that’s right — if I can avoid using ContentProviders, I do, mainly for this reason. Of course you could do a join in your content provider too if columns are requested from multiple tables.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can also subscribe without commenting.