android contentProvider notifyChange - android

I am baffled by notifyChange machenism used in contentProvide:
// Tell the cursor what uri to watch, so it knows when its source data changes
c.setNotificationUri(getContext().getContentResolver(), uri);
and
getContext().getContentResolver().notifyChange(noteUri, null);
Here are the questions (suppose the provider and the client are in different packages):
Is the contentResolver returned by Provider the same as Resolver returned by the client?
Is the cursor the same as returned by the provider and the client?
what is the Uri resolution to be notified changes? the entire table uri or a row?
Please clarify

I know this is an old question. I am answering so that it is helpful for someone who comes across this question.
Is the contentResolver returned by Provider the same as Resolver returned by the client?
Yes, it is the same.
Is the cursor the same as returned by the provider and the client?
I suppose, you are asking this in the context of your first code snippet which is seen inside query() method. Here, you are setting the notification uri on the cursor which is to be returned to the client. So, obviously, the client and the provider are using the same cursor.
what is the Uri resolution to be notified changes? the entire table uri or a row?
Uri for notification depends on the operation:
insert() - The notification Uri is the table Uri appended with the
id of the newly inserted row.
delete() and update() - Here, the Uri is the table Uri appended with the id of the deleted or updated row.

Related

Content URI TO Android Room (SQLite conversion)

I am given a task where I need to change the activity. The Activity uses Cursor Loaders, Cursor and Content Uri.
Uri tempUri = some function call; That calls // BASE_CONTENT_URI.buildUpon().appendPath(PATH_ROUTES).build();
This will give
content://com.companyname.android/route/routeID/stops/sRcH
My task involves converting this URI to an SQL Query. How can I do that? I have a little idea about cursor since I have worked mostly in Room DB. How can I convert this into a query? I am unable to convert this into a SQL Query because I am not able to figure out what is the database in this, what is the table name or subtable name. Help will be appreciated. Thanks.

Distinct Query For CursorLoader

I am using CursorLoader to populate my listView.
But I need to get distinct result set.
But CursorLoader has no constructor to set distinct parameter.
Any Suggestions?
Yes!
In the CursorLoader constructor you specify a URI. Part of that URI is the ContentProvider Authority. The rest, though, is up to you! So create a virtual table!
If you have a table "songbirds", perhaps your URI is:
content://my.content.provider/songbirds
Simply teach your ContentProvider (add one more clause to your UriMatcher) to recognize the URI:
content://my.content.provider/distinctSongbirds
Process the "distinct" URI exactly as you do the non-distinct one, except add a "distinct" param to the actual DB query, when you make it.

How to use URI to edit multiple tables in the same database

I have a database and within the database i have 3 tables but im using a content provider so how can i distinguish between the tables? i understand the format of the uri and that i would have to use a separate path for each but how do i get the value of the uri with the different paths. do i need to use uri matcher and then that will return the uri i want based on what i put in the parameters? from what i found online it looked like uri matcher was only used for the get type method in the content provider.
do i need to use uri matcher and then that will return the uri i want
based on what i put in the parameters?
Correct. Each of the ContentProvider's methods is called with a Uri parameter. You can simply pass that to your UriMatcher and then continue in a switch statement.

How to properly insert values into the SQLite database using ContentProvider's insert() method through a CursorLoader?

I was reading the doc, but I am still not too sure. Its says to use getContentResolver(), but then that really isn't using CursorLoader. So is there a way to do it through CursorLoader? I know how to do it with query(). Are the steps very similar? Even just a link that explains exactly this would be helpful.
Please note, do not link me to the Google doc as they do not have an example that ever uses the insert() method from ContentProvider using a CursorLoader.
Thanks in advance!!
Edit: I should probably mention the reason I am confused with this is because calling a new CursorLoader automatically calls ContentProviders query() method. But how can I do the same for insert?
Check out my blog post on the subject:
Content Resolvers and Content Providers
The CursorLoader has nothing to do with it.
Insertion is a totally different concept... it has absolutely nothing to do with the CursorLoader. When coupled with the LoaderManager, the CursorLoader automatically queries your database and updates itself when the ContentObserver is notified of a datastore change. It has nothing to do with the actual process of inserting data into your database.
How requests to the ContentResolver are resolved
When you insert (or query or update or delete) data into your database via the content provider, you don't communicate with the provider directly. Instead, you use the ContentResolver object to communicate with the provider (note that the ContentResolver is a private instance variable in your application's global Context) . More specifically, the sequence of steps performed is:
You call getContentResolver().insert(Uri, ContentValues);
The ContentResolver object determines the authority of the Uri.
The ContentResolver relays the request to the content provider registered with the authority (this is why you need to specify the authority in the AndroidManifest.xml).
The content provider receives the request and performs the specified operation (in this case insert). How and where the data is inserted depends on how you implemented the insert method (ContentProvider is an abstract class that requires the user to implement insert, query, delete, update, and getType).
Hopefully you were able to wrap your head around that at least a little. The reason why there are so many steps involved is because Android (1) allows applications to have more than one content provider, and (2) needs to ensure that apps can securely share data with other third-party apps. (It wasn't because it wanted to confuse you, I promise).
Inserting data via the ContentProvider
Now that you (hopefully) have a better idea of how the ContentResolver is able to relay these requests to the content provider, inserting the data is fairly straight forward:
First, decide which uri you want to have matched by your content provider. This depends on how you decided to match your uris with the UriMatcher. Each uri you have represents a different means of inserting data into your internal database (i.e. if your app has two tables, you will probably have two uris, one for each table).
Create a new ContentValues object and use it to package the data you wish to send to the content provider. The ContentValues object maps column names to data values. In the below example, the column name is "column_1" and the value being inserted under that column is "value_1":
ContentValues values = new ContentValues();
values.put("column_1", "value_1");
Once received, the content provider will (in your case) pass the values object to your SQLiteDatabase (via the SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values) method). Unlike the ContentProvider, this method is implemented for you... the SQLiteDatabase knows how to handle the values object and will insert the row into the database, returning the row id of the inserted row, or -1 if the insertion failed.
... and that's how you insert data into your database.
TL;DR
Use getContentResolver().insert(Uri, ContentValues);
Convert Cursor to ContentValues for easy database insertion.
Its says to use getContentResolver(), but then that really isn't using CursorLoader
Besides what Alex said, there's nothing preventing you from iterating through the Cursor returned, putting them into ContentValues and then inserting that (say, to a different DB).
Just an example from the top of my head (a simple Cursor of two String columns):
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor.moveToFirst()) {
ArrayList<ContentValues> values = new ArrayList<ContentValues>();
do {
ContentValues row = new ContentValues();
DatabaseUtils.cursorStringToContentValues(cursor, fieldFirstName, row);
DatabaseUtils.cursorStringToContentValues(cursor, fieldLastName, row);
values.add(row);
} while (cursor.moveToNext());
ContentValues[] cv = new ContentValues[values.size()];
values.toArray(cv);
getContentResolver().bulkInsert(CONTENT_NAMES, cv);
}
}
There are probably more efficient ways to do that (and definitely some integrity checks), but that was my first thought...

How to delete the contacts on android 2.2?

I need to delete the duplicate contacts and then inserting the new contact on Android 2.2.
How to do this?
give me any sample code or sites for this.
To delete the content item from android you need a content URI and some deletion criteria.
Every content type has its own content URI. If you're writing your contacts sync adapter, then you might want to use ContactsContract.RawContacts.CONTENT_URI.
Other thing you need is a ContentResolver - an interface to communicate with a content provider (an operations, such as insert, update and delete are defined in this interface). You can get ContentResolver by calling getContentResolver from your application context.
So, here is the snippet of code that should delete ALL the contacts (not tested it though):
ContentCesolver cr = getContentResolver();
URI uri = RawContacts.CONTENT_URI;
cr.delete(uri, null, null);
Note that when you're using RawContacts.CONTENT_URI, the contact item is not deleted. Instead it is only marked for deletion. To delete it completely you should add a ContactsContract.CALLER_IS_SYNCADAPTER parameter to your URI:
uri.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,
"true").build()
For further explanation read official docs about content providers.
May be this will help you,
How to remove a contact programmatically in android

Categories

Resources