在Android的ContentProvider的连接表,并通知ContentObservers最佳实践 [英] Best practices for joining tables and notifying ContentObservers in Android ContentProvider
问题描述
我有处理所有的数据插入和检索有关我的申请,我下面就谷歌I / O建议的维吉尔Dobjanschi模式ContentProvider的。我使用的是第一种模式。
I have a ContentProvider which handles all the data insertion and retrieval related to my application, I'm following the pattern suggested by Virgil Dobjanschi on Google I/O. I am using the first pattern.
我的问题是,我有一个重新由多个数据库中的表psented $ P $的逻辑实体。
My problem is that I have a logical entity that was represented by multiple tables in the database.
例如,我有一个文章表和ArticleExtras表。文章重新presents文章本身,而ArticleExtras重新presents对某些文章,如评论数addtional信息。
For example, I have an Articles table and an ArticleExtras table. Articles represents the article itself, while ArticleExtras represents addtional information about certain Article, like number of comments.
我用的CursorAdapter在UI中显示文章标题及该条评论中的ListView一行的数量。
I used CursorAdapter in the UI to display the Article title and the number of comments of that Article in one row of ListView.
要实现这一点,我添加了一个左外连接ArticleExtras上
语句中的第表我的ContentProvider查询的方法,以便CursorAdapter的获得ArticleExtras与文章本身
To implement that, I added a left outer join ArticleExtras on
statement in my ContentProvider query method for Article table, in order for CursorAdapter to get ArticleExtras along with the Article itself.
在新的文章是从网上获取的,我把它插入通过ContentProvider的数据库,然后CursorAdapter的得到了通知,并更新UI,这部分工作正常。
When new Articles are fetched from the web, I insert it into the database via ContentProvider, and then the CursorAdapter got notified and update the UI, this part worked as expected.
但是当我拿来的意见(ArticleExtras)的数量,我想同样的CursorAdapter,这是看的变化内容://com.myapp.content/Articles
,被通知,也让我可以在ListView更新我行。
But when I fetched the number of comments (ArticleExtras), I want the same CursorAdapter, which is watching for changes in the content://com.myapp.content/Articles
, to be notified, too, so I can update my row in the ListView.
我目前的执行情况是这样的:插入ArticleExtras到数据库后,我开始一个新的查询,以检查是否条表具有一个关系到我刚插入的ArticleExtras任何行。如果是的话,我会做一个新的URI为第(例如:内容://com.myapp.cotent/Articles/123
),并调用的getContext()。getContentResolver()。有NotifyChange(URI,空)
,这样在看这条修改相应的CursorAdapter会得到通知。
My current implementation is like this: After inserting ArticleExtras into the database, I start a new query to check if Articles table has any rows that is related to the ArticleExtras I just inserted. If so I'll make a new uri for that Article( for example: content://com.myapp.cotent/Articles/123
), and call getContext().getContentResolver().notifyChange(uri, null)
, so the corresponding CursorAdapter that is watching for changes of this Article will get notified.
时的做法正确,或者是还有什么更好的办法来实现我想要什么?
Is the approach correct, or is there any better way to implement what I want?
推荐答案
结帐<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/android/providers/contacts/ContactsProvider2.java"相对=nofollow> ContactsProvider2 ,在它自己设置通知的URI的 AUTHORITY_URI
这似乎是一个包罗万象的所有在提供其他的URI。我有同样的probem,我已经试过此我自己的供应商有多个表和联接这些表,并能正常工作。
Checkout ContactsProvider2, in it they set the notification uri to the AUTHORITY_URI
which appears to be a catch all for the other URIs in the provider. I had the same probem and I have tried this myself for a provider with multiple tables and joins on those tables, and it works fine.
这篇关于在Android的ContentProvider的连接表,并通知ContentObservers最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!