How does the content provider uri work? - android

I've been researching ContentProviders for a while now and I'm still having trouble grasping how exactly the Uri works.
content://app_name/path/id
When a path is specified how does the table corresponding to that path gets accessed?
It seems like you would need to label the table with that path. Is it so?

You are right google docs on this point are not very clear :
First : you must define an authority for a custom provider :
http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI
<provider
android:name=".ProviderDemo"
android:authorities="com.foo.android.providerdemo" />
Second : use this authority when you query a provider :
http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI
//query all items
getContentResolver().query("content://com.foo.android.providerdemo/",...)
//query a single item
getContentResolver().query("content://com.foo.android.providerdemo/idOfItem>",...)
//query a view
getContentResolver().query("content://com.foo.android.providerdemo/<idOfView>/",...)

To help you choose which action to take for an incoming content URI, the provider API includes the convenience class UriMatcher, which maps content URI "patterns" to integer values. You can use the integer values in a switch statement that chooses the desired action for the content URI or URIs that match a particular pattern.

My confusion came from a lack of SQLite knowledge. I was unaware that a database holds many tables and not just 1 table. Now I realize that the authority of the Uri must take you to the database and then the path will take you to the correct table.

Related

Confused about Content provider contenturi and content provider authorities tag

from google example below
As an example of designing and coding content URI handling, consider a provider with the authority com.example.app.provider that recognizes the following content URIs pointing to tables:
content://com.example.app.provider/table1: A table called table1.
content://com.example.app.provider/table2/dataset1: A table called dataset1.
content://com.example.app.provider/table2/dataset2: A table called dataset2.
content://com.example.app.provider/table3: A table called table3.
I am confused here. Does this example mean that Table1Contentprovider, Table2ContentProvider, Table3ContentProvider are pointing at the same authority url? From what I read, each provider should have unique authority url.
Or does it mean, there is only one provider here that works all 3 tables? If that is the case, in the query method of the provider do we run sql query to get data related to all three tables?
from google example below
Actually, it is from this page, not the one you linked to.
Does this example mean that Table1Contentprovider, Table2ContentProvider, Table3ContentProvider are pointing at the same authority url?
There is only one provider, not three ("consider a provider").
From what I read, each provider should have unique authority url.
A ContentProvider can support multiple authorities, via a comma-delimited list in the android:authorities attribute, though most of the time they only support one.
Or does it mean, there is only one provider here that works all 3 tables?
Correct.
If that is the case, in the query method of the provider do we run sql query to get data related to all three tables?
In methods like query(), you examine the Uri that you are supplied and perform the operations specific to the supplied path. In the aforementioned example, query() would look to do different things for the paths table1, table2/dataset1, table2/dataset2, and table3.

What does BASE_PATH of CONTENT_PROVIDER really means?

I'm trying to build a Content_Provider but i'm stuck on Base_path....
For example I have table name arrival like below :
How can I name a CONTENT_URI ?
Can anyone help me out ?
Thanks in advance !
From the docs:
A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority)
and a name that points to a table (a path). When you call a client
method to access a table in a provider, the content URI for the table
is one of the arguments.
In the preceding lines of code, the constant CONTENT_URI contains the content URI of the user dictionary's "words" table. The
ContentResolver object parses out the URI's authority, and uses it to
"resolve" the provider by comparing the authority to a system table of
known providers. The ContentResolver can then dispatch the query
arguments to the correct provider.
The ContentProvider uses the path part of the content URI to choose the table to access. A provider usually has a path for each
table it exposes.
In the previous lines of code, the full URI for the "words" table is:
content://user_dictionary/words
where the user_dictionary string is the provider's authority, and words string is the table's path. The string content:// (the scheme)
is always present, and identifies this as a content URI.
So in a content_uri, you have an authority and a base_path. The ContentResolver will use the authority part to decide which provider to choose and then the base_path part to decide which table to provide the data from. So, simplifying, the base_path is usually the path for your particular table within your provider.
Refer this guide for more and go through this tutorial as well.

SearchManager - adding custom suggestions

I've read all of the documentation online about building search interfaces and adding custom suggestions... but I'm still unclear on how this works. The documentation says that I must "Build a table (such as in an SQLiteDatabase) for your suggestions and format the table with required columns". I'm assuming the system will eventually fill this table with the appropriate suggestions on its own... but which process/class is responsible for this, and when will the actual insertions occur (before any query is made by the user, after a query has been made by the user, etc.)?
And while I'm asking a question up here, if someone could clarify the difference between an AutoCompleteTextView and a SearchView w/ custom suggestions... that'd be awesome. AutoCompleteTextView seems suspiciously easy to implement compared to the SearchView (which requires changes to be made to the ContentProvider, SQLiteDatabase helper class, etc.).
You have to create a content provider which delivers your custom suggestions based on a query so far entered in the search view. In your searchable.xml you configure the minimum length of the search expression, which must be reached before asking for suggestions. This content provider is called a suggestion provider (it still extends ContentProvider). The content provider's authority is also configured in searchable.xml.
There is no limitation on how the suggestion provider computes its suggestions. You can search the web query a database or read a file. But the answer to the query is in the format of a table. If the suggestions is directly queried from a database you can use the cursor answered by the database query to deliver the result in the content provider's query() method. If the result is computed from one or more sources you can create a table on the fly by using a MatrixCursor.
The rows of the answer from the suggestion provider are used by the search mechanism to display the suggestion, they are stored in a table. The format of the rows is as follows:
private static final String[] COLUMNS = {
"_id",
SearchManager.SUGGEST_COLUMN_ICON_1, // ID of a drawable (icon) as String
SearchManager.SUGGEST_COLUMN_TEXT_1, // main text for suggestion display
SearchManager.SUGGEST_COLUMN_TEXT_2, // secondary text for suggestion display
SearchManager.SUGGEST_COLUMN_INTENT_DATA, // this could be an URI to access the suggestion as used in an intent with a VIEW action
SearchManager.SUGGEST_COLUMN_INTENT_ACTION, // this could be Intent.ACTION_VIEW
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID // e.g. SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT
};
Searching is described here in more detail: http://developer.android.com/guide/topics/search/index.html

Content Provider filtering query, filtering Cursor

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.

How to structure URI query for multiple values for same key?

I'm trying to structure a URI that accesses my data by id.
Currently my URIs are query based like so:
.../content?parentList=15&type=note
How could I structure a similar URI so that I could query for notes in multiple lists?
Essentially combining the results of the next two URIs.
.../content?parentList=15&type=note
.../content?parentList=16&type=note
Is there a standard way to do this?
According to the current draft of the URI Template spec (Section 3.6 on page 7), you could do:
.../content?parentList=15,16&type=note
The Query portion of the URI doesn't require each parameter to occur only once. Just repeat the query:
.../content?parentList=15&parentList=16&type=note
It is also part of the URI template internet draft:
{?list+}
becomes
?list=val1&list=val2&list=val3

Categories

Resources