I'm designing a ContentProvider which needs to return a cursor with data and I need to know the status of that data.
I need a status for the cursor data since this data was downloaded and I need to know if it only downloaded partial data or if during the download some of the elements weren't downloaded. It's a bit more complex but you might get the idea why I need a status for my cursor data.
So I have reviewed the source code for all Cursor implementations in Android and it seems getExtras and respond functions are plain garbage since they don't do anything internally... Also, there seems to be missing a setExtras function. Any ideas?
I think that the method is not setExtras(), but respond().
I'm not sure what you mean by the "status of the data", but it sounds like you want to know if the columns in the cursor are clean or not. Most content provider implementations I've seen put this type of status into the rows themselves, or store it in a separate table. I don't think you should try attaching the status to the Cursor itself.
Just so you know, if you want to get notification when the data underlying the cursor changes, you can use setNotificationUri(), registerContentObserver, or registerDataSetObserver().
Notice that nothing requires you to make the columns you provide from a content provider be the same as the columns in any underlying database you use. You could add a column that the provider itself generates, and store status in that.
Related
I have a HTTP Server and Android app.
All data -> JSON format. For mapping used Gson.
ORM - ActiveAndroid.
The problem : I need something like an observer/notifier object, which can told me, that a row in database updated just now.
Something like this :
public void interface Observable<T> {
void onItemUpdated(T item);
}
So I looking a solution. I've read ActiveAndroid docs, but it doesn't get to me any result. Maybe i can mix something with ContentObserver or something like this?
How about to create a logging table where you can append a row for each update.
It has the insertion cost but select query is so fast. And also you can store update history of each record if you want or delete the log record if you are worry about performance.
you can create new field in your db which holds the updated date and time and after every operations in db you may query the db about the recent updates done to your db
You can accomplish this by implementing your own ContentObserver for the URI of the table you want to be notified about. ActiveAndroid has its own content provider you can use or you can implement your own, that works with your tables. In one of my projects I used ActiveAndroid and had my own content provider for 3rd party apps. Upon an update/insert/deletion of a row you should be notified by content provider that something was changed with reference to some URI and if you have a registered content observer to this URI you will be notified.
I have a Android app which has a Content Provider which I update frequently. To load the lists of the said data, I'm using a LoaderManager.
On the detail page, I also use a LoaderManager, but I create the CursorLoader like this:
return new CursorLoader(getActivity(), DataProvider.POST_URI, projection, "_id = ?", new String[]{String.valueOf(post_id)}, null);
As you can see, I'm only getting a single item from the table. When the row is updated the the onLoadFinished callback is updated, but, also, when other rows are added/modified to the table. It wouldn't be much of a hassle, but sometimes, it causes the whole list to get blank and redrawn, which can be frustrating for the user when it happens multiple times.
Do you know how could I avoid this behavior?
The reloading is taking place because you are associating this cursor loader with the DataProvider.POST_URI which I assume is the one you use for your posts table. Whenever there is a change in any of the elements in this table, all the cursors with that Uri will be notified. If you want to receive notification of a single item you should implement a new Uri scheme to match just that one item. Have a look at the documentation for the UriMatcher class for an example. You should do something similar to the PEOPLE and PEOPLE_ID cases.
EDIT
You can find a good tutorial on ContentProvider here. Section 9.4 in special should be helpful in correctly implementing the ContentProvider.
I want to write an app that loads in a load of words from a content provider. Then i want to update certain fields associated with a word, ie view count etc, one at a time
What is the best approach to accessing the data.
So first i load all the words into a Cursor object, when i want to update a word view-count do i just call an update event . Will this update just the Content provider or will it also update the fetched results that are stored in the Cursor
After a word is viewed i want to remove it from the Cursor object so it wont be shown again, is that possible or do i need to do a full query to get all the words again
sorry if vague or hard to understand
I am using a ContentProvider for caching results from a web-service query. It is an HTTP request and the response content is XML. Most of the data is cached, so I simply query the DB, if not found, request from webservice, insert in DB and requery the DB. Thus the response is always a Cursor from SQLiteDatabaseHelper.
I have one result set that is not stored in the DB and since it is 100% transient, but I would like to provide the appearance of it coming from the DB's Cursor. Is there an easy way to do this? For example, if I could project it onto the cursor with a cursor.setValue("string", objectValue) or some other existing implementation.
If not, I will either bypass the DB for this content result, or stuff it into a trivial table that is constantly reused.
Depending on how you use it, it might not be too hard to write your own cursor class. For convenience, derive your class from AbstractCursor class which takes care of a lot of the details for you.
You may also be able to make use of MatrixCursor.
I got following problem, I need to use a Content Provider to read a
Database of an other App.
first I want all rows, and after analyzing the data only e.g. the rows
from _id = 1, 3 and 5.
how can I call a Content provider and select only these rows?
or is it possible to create a subset Cursor form an given Cursor?
Thanks in advance.
If you're talking to another app, I assume you're querying the other app's ContentProvider to get the data from them in the first place.
In this situation, the cleanest answer seems not to build your own ContentProvider that filters/wraps theirs. Instead query their ContentProvider from your application directly, and use the select clause in your query() to specify the conditions that define the subset of data you want to be given.