Android content provider and many-to-many database

I have a simple Notes application that is similar in functionality to the NotePad sample for Android. One of them is that each note can have tags. A Note can have multiple Tag , and Tag can belong to multiple Note , which makes it a many-to-many relationship.

I completed the development of the database using foreign keys and a mapping table. Now I want my application to connect to the Android Search Framework, which requires the use of ContentProvider to disclose my data.

Are there any recommendations for this scenario? I found some related questions on SO, but most of them dealt with a one-to-many relationship ( this one ). On these issues, I concluded that it is best to have one ContentProvider for each database, and then use the Matcher concept to solve multiple tables in the database. This still leaves open other questions.

  • Given the Note ID , I would like to return all the tags associated with this note. How do I configure ContentUri for this case? None of "content://myexample/note/#" and "content://myexample/tag/#" will serve the purpose.

  • None of the 6 ContentProvider methods that I override are suitable for this purpose, are they? I can, of course, introduce a new method, but this will not be understood by consumers of my ContentProvider .

Thanks in advance for your suggestions.

+7
source share
2 answers

Yes, you need to implement it using ContentProvider if you want to implement a search.

You can use an existing content provider to retrieve tags associated with the scrap.

you can define a different URL in the form "content: // myexample / note / tag / #" to get all the tags associated with a particular node.

You will need to add another URI, which must be mapped in your URI agreement. let's say something like GET_NOTE_TAGS = 3;

In the getType method of your ContentProvider, return the corresponding mime type. I would suggest that this would be the same as the mime type for tags, since you are returning the same tags.

Then, in your query / update / delete / insert methods, parse the incoming URI so that it matches "content: // myexample / note / tag / #". Then, appropriately fulfill your request in your database or your content and return the tags you want to return.

The scheme "content: // myexample / note / tag / #" is just an example, and you can use multiple URIs within the same ContentProvider.

You also stated that you need to join the tables. this can be done using db.query, and if it is difficult, you can use rawqueries to get data as needed from the database.

+5
source

Now I find some cool many-to-many stuff on Android ContentProvider. The answer comes from the source code of Google Android I / O 2011 Offcial Android Client. For example, a Google I / O application has an entry called Session , and another entry is Speaker . One Session can have several Speaker , and one Speaker will attend several Session .

So let's take a look at Google's solution:

https://github.com/google/iosched/blob/2011/android/src/com/google/android/apps/iosched/provider/ScheduleProvider.java

https://github.com/google/iosched/blob/2011/android/src/com/google/android/apps/iosched/provider/ScheduleContract.java

https://github.com/google/iosched/blob/2011/android/src/com/google/android/apps/iosched/provider/ScheduleDatabase.java

Perhaps this answer will help you guys.

+9
source

All Articles