Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Content Provider and Many-to-Many DB relationship

I have a simple Notes app which is similar in functionality to the Android NotePad sample. One addition is that every note can have Tags. A Note can have several Tags and a Tag can belong to several Notes - thus making this a many-to-many relationship.

I have completed the DB Design using Foreign Keys and a Mapping Table. Now, I wish my app to plug into the Android Search Framework, which necessitates the use of a ContentProvider to expose my data.

Are there any best practices to be followed for this scenario? I did find a few related questions on SO, but most of them dealt with one-to-many relationships (this one for example). I did conclude from these questions that it is best to have a single ContentProvider per DB, and then use the Matcher concept to resolve multiple tables within the DB. That still leaves other questions open.

  1. Given a Note ID, I would like to return all the tags associated with that Note. How do I go about setting up the ContentUri for such a case? Neither of "content://myexample/note/#" and "content://myexample/tag/#" would serve the purpose.

  2. None of the 6 ContentProvider methods which I override would suit such a purpose, would they? I can of course, introduce a new method, but that would not be understood by the consumers of my ContentProvider.

Thanks in advance for your suggestions.

like image 255
curioustechizen Avatar asked Jul 05 '11 03:07

curioustechizen


1 Answers

Now I find some cool stuff about the many-to-many entry relationship in Android ContentProvider. The answer comes from the Google I/O 2011 Offcial Android Client source code.For example, in the Google I/O app, there is a entry called Session and another entry is Speaker. One Session maybe have several Speaker and one Speaker will attend several Session.

So,let's have a look about the 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

Maybe this answer will help you guys.

like image 112
penkzhou Avatar answered Sep 27 '22 17:09

penkzhou