Android: How to Use Two Data Sources in ListViews?
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.
-
You get a list of all elements of the
ListView
by querying your database. And you query the content provider for each list entry using anAsyncTask
. This is the a very memory efficient solution.For images (album covers, contact pictures...) this is probably the best option. Especially if combined with an in-memory cache. Even if users would love to see images immediately, they know this is not feasible and accept some lag before images are displayed.
But not so for any text elements. The second line of a multiline list element should be visible instantaneously. So for text this option is not acceptable.
-
You could query the content provider for all entries at startup, put them in a HashMap and use this map later on. Well this might work if the content provider has only a few entries. But there might be thousands of media files on a SD card or thousands of contacts for a big company. In cases like these this won't work. It would be a big waste of memory - especially since you probably only need very few of these rows at all.
The Hashmap would also need too long to be created in the first place. The waiting period for the inital screen would be inacceptable. So this is basically a no-go.
-
You could keep redundant data in your own database. For every record that contains a link to the external content provider you also include the data, you need in the listview (e.g. the band name). That's the option I have chosen. You only cache the results you actually need.
There are two drawbacks to this solution: First you are wasting device memory. That is flash memory not RAM. No problem for most high end devices but low cost devices often have very limited memory available.
Second you have to sync the data whenever your app restarts. If your app stays in the background for quite a while, the user might have changed the external data source. In fact this might even happen while your app is in the foreground if a sync adapter changes values based on changes of the backend. Now your list reflects the wrong values until your database has re-synced with the external provider.
You could choose to listen to changes in the content provider in a service and update your database when needed. Activities then could query for changes.
Whether these two issues of this option pose problems for you depend on the type of app. In most cases a correction of a changed value later on should be okay. And if you do not have hundreds or thousands of rows in your own table the inefficiencies are not that bad as well.
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.
相關推薦
Android: How to Use Two Data Sources in ListViews?
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
[轉]How to display the data read in DataReceived event handler of serialport
本文轉自:https://stackoverflow.com/questions/11590945/how-to-display-the-data-read-in-datareceived-event-handler-of-serialport 問: I have the followin
How to use *args and **kwargs in Python
這篇文章寫的滿好的耶,結論: 1星= array, 2星=dictionary. 1星範例: def test_var_args(farg, *args): print "formal arg:", farg for arg in args: print "an
Listen: Baratunde Thurston Reads "How To Do A Data Detox In a Zillion Easy Steps"
Baratunde Thurston: “How To Do a Data Detox In a Zillion Easy Steps”Moving beyond alarmist headlines, Baratunde exposes the plain truths about the fight fo
How To Use Retrofit Library In Your Android App
Retrofit library is a Type-safe REST client for android and Java, courtesy of Square Inc. Most modern android apps make HTTP requests to some remote s
How to Use Loaders in Android
With the introduction of Honeycomb Loaders became the preferred way to access data of databases or content providers.
How To Use Simple Factory Design Pattern In Java
Simple Factory Design Pattern is one of the many design patterns – best practices on how to solve common problems. Design Patterns were made popular by
Why (and how) to use eslint in your project
Why (and how) to use eslint in your projectThis story was written by Sam Roberts, a Senior Software Engineer at IBM Canada. It was first published in IBM d
how to use “request” object within a function in jsp
request is accessible inside the scriptlet expressions, because it’s an argument of the method in which these expressions are evaluated (_jspService). But
How to use AI in the insurance value chain: customer service and policy administration
How do you approach customer service and policy administration within your organization? In this blog post, I'll demonstrate how artificial intelligence (A
How to use APIs with Pandas and store the results in Redshift
How to use APIs with Pandas and store the results in RedshiftHere is an easy tutorial to help understand how you can use Pandas to get data from a RESTFUL
How to use AI in a small business
If you are a small business owner, you may wonder if it is possible to implement AI in your business; you may even feel very challenged by the idea. There
How to Use IoT Datasets in #AI Applications
Recently, google launched a Dataset search – which is a great resource to find Datasets. In this post, I list some IoT datasets which can be used for Machi
How to use Git in a secure way
How to use Git in a secure wayWe live in a world where it is hard not to know Git, the most popular Distributed Version Control System (DVCS). Free and ope
How to use DeepLab in TensorFlow for object segmentation using Deep Learning
How to use DeepLab in TensorFlow for object segmentation using Deep LearningModifying the DeepLab code to train on your own dataset for object segmentation
Subclassed: how to load initial data and test data in Django 2+
There are two ways to automatically load data in Django: for data you need while running tests, place xml/json/yaml files in yourapp/fixtures. for data
How to use Android Studio's SVG-to-VectorDrawable converter from the command line
Since the very beginning, one of the most annoying aspects of using VectorDrawables on Android has been the lack of a reliable SVG converter. Goog
10 Examples of How to Use Statistical Methods in a Machine Learning Project
Tweet Share Share Google Plus Statistics and machine learning are two very closely related field
How to Use Power Transforms for Time Series Forecast Data with Python
Tweet Share Share Google Plus Data transforms are intended to remove noise and improve the signa