I had not copied the code here, but re-wrote it in the post. This miss was because I made mistake while typing.
And I got -1 cause my content provider was getting registered. It was not a problem Now I will write the test case as you said and it will also help me fix the code properly. Basically, we have code base which I have is written in Donut and I am helping my customer update it with few new features and run it in both Eclair and Froyo. And also move it to Gingerbread once it is out... Providing ContentProvider is a big requirement as app never shared the data and had local app db. Regards, Arpit On Sep 10, 12:19 am, "A. Elk" <[email protected]> wrote: > I missed the mismatch as well; it's subtle. In your manifest, you set > the authority to com.arpit.providers.MyContentProvider, while in the > code you set it to com.arpit.provider.MyContentProvider. Note the > missing "s" for provider in the code. The ContentResolver shouldn't be > able to resolve your provider, so you should always get back null when > you call it. This rouses my curiosity; how do you know that UriMatcher > is always returning -1? > > On the whole, it would be better in this case to do some debugging > first. If you provide values to a function, but it doesn't act as you > expect, then either you passed in the wrong values or misunderstood > the requirements. Check the values for correctness before you assume > you misunderstood something. > > Content provider users should also consider writing unit tests for > their providers. ProviderTestCase2 is a good, comprehensive way to > test a provider in isolation. > > elk. > > On Sep 9, 11:15 am, Brion Emde <[email protected]> wrote: > > > > > You should not hijack existing threads with new questions that do not > > correspond to the original thread. That makes it very difficult for > > people to find the original thread. Instead, you should start a new > > thread with your question. > > > That said, I see that the value of the AUTHORITY that you declare in > > your ContentProvider do not match with the android:authority that you > > declare in your AndroidManifest.xml. You should check that they match. > > > Finally, rather than giving snippets of code that you think might be > > useful, it is actually more useful if you give a full example. > > > On Sep 9, 1:21 am, Arpit <[email protected]> wrote: > > > > Somehow my UriMatcher is not working properly and because of that I am > > > not able to do the CRUD operations on my tables. > > > > I have the following structure of packages and classes: > > > > com.arpit.provider : This has a class MyContentProvider which extends > > > ContentProvider class > > > com.arpit.tables : > > > DatabaseHelper extends SQLiteOpenHelper > > > KeyTable implements BaseColumns > > > ParticipantTable implements BaseColumns > > > > Now in MyContentProvider, I have done the following: > > > > ... > > > public static final String AUTHORITY = > > > "com.arpit.provider.MyContentProvider"; > > > ... > > > static{ > > > UriMatcher uriM = new UriMatcher(UriMatcher.NO_MATCH); > > > uriM.addUri(AUTHORITY, "key", 0); > > > uriM.addUri(AUTHORITY, "participant", 1);} > > > > ... > > > public Cursor query(Uri uri, String[] projection, String selection, > > > String[] selectionArgs, String sortOrder) { > > > SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); > > > switch(uriM.match(uri)){ > > > case 0: > > > qb.setTables("key"); > > > break; > > > case 1: > > > qb.setTables("participant"); > > > break; > > > } > > > ....} > > > > ... > > > > In my KeyTable class, CONTENT_URI = "content://" + > > > MyContentProvider.AUTHORITY + "/key"; > > > In my ParticipantTable class CONTENT_URI = "content://" + > > > MyContentProvider.AUTHORITY + "/participant"; > > > > In my AndroidManifest.xml file I have registered the provider as: > > > > <provider android:name="com.arpit.providers.MyContentProvider" > > > android:authorities="com.arpit.providers.MyContentProvider"></ > > > provider> > > > > Now I make the query call with the following statement in my > > > HomeActivity: > > > > Cursor cursor = getContentResolver().query(KeyTable.CONTENT_URI, new > > > String[] { KeyTable.col1 }, "_id='1'", null, null); > > > > My problem is when the Query Method is called and the Switch block is > > > executed the uriM.match returns always -1 (irrespective of whether it > > > is KeyTable.CONTENT_URI or ParticipantTable.CONTENT_URI). > > > > Could you let me know what wrong am I doing because of which it is not > > > working. > > > > From the note pad example I see during debug the only difference is > > > they call managedQuery instead of getContentReslover().query(...). I > > > tried firing managedQuery(...) in my HomeActivity class, but that also > > > result in uriM.match(uri) to return -1. > > > > It will be great if you could help me out. > > > > Thanks & Regards, > > > Arpit > > > > On Aug 25, 10:20 pm, "A. Elk" <[email protected]> wrote: > > > > > The Note Pad example is pretty good for URI content matching. > > > > > Basically, you define one or more URIs for a ContentProvider. Don't > > > > think of them as "URLs", just think of them as strings with a > > > > particular format. Using web domains in them is a easy, nearly > > > > foolproof way to ensure that each URI is unique. For example, I can > > > > safely use the content URI > > > > content://database.lancaster.dambusters.gmail.com > > > > because that's my unique GMail address. > > > > > Patterns come in when you want a ContentProvider to return different > > > > things depending on the exact URI. In the Note Pad sample (*not* the > > > > tutorial), there are 3 patterns corresponding to 3 "forms" of data: a > > > > list of notes, a single note, or a set of notes compatible with the > > > > LiveFolder widget. The "list of notes" form is set up in the code and > > > > the manifest so that it returns a set of records. The single note > > > > returns exactly one note, and I forget offhand what LiveFolders does. > > > > Let's look at the first two. > > > > > For set of notes, you just specify a "base" URI. The Provider returns > > > > all the notes in the database that match your criteria. > > > > > For a single note, you specify a pattern that is the "single note" > > > > base URI with a note ID number appended to it. The ID number is the > > > > value of the "_ID" column for the record you want. The Provider > > > > returns that particular note. As written, the Provider can also filter > > > > the note by selection criteria. > > > > > If you look in the code for the Provider, you'll see that it defines > > > > aUriMatcher. You useUriMatcher.addUri() to match an "authority" (a > > > > base URI without a specific pattern) and a pattern to a value. When a > > > > URI comes in to the query() method, you useUriMatcher.match to match > > > > the incoming URI to a pattern. The method returns the value that you > > > > associated with the pattern in addUri(). TheUriMatcheris defined in > > > > a static block at the end of the code. > > > > > As a note, you can use "?" in a pattern as a wildcard to match any > > > > string, and "#" to match any number. The "#" is used to match a single > > > > note in Note Pad. > > > > > The Note Pad code is hard to understand because it's designed to > > > > accept alternate actions. This allows other apps to access the > > > > ContentProvider by specifying the proper authority and MimeType. You > > > > can ignore this, basically. The mime type tells the calling app what > > > > it's gonna get back from the Provider. If the type is > > > > vnd.android.cursor.dir, then the calling app knows it may get back > > > > more than one row, whereas if it gets back cursor.item, it gets back > > > > only one item. Incoming Intents have to specify a mimeType so that > > > > they exactly match Note Pad's intent filters. For example, an app that > > > > sends an Intent with action.GET_CONTENT but mime type > > > > vnd.android.cursor.dir won't match any of Note Pad's intent filters. > > > > > The elkmeister. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

