自动更新Cursor
s的习惯用法和imho正确的方法是在创建它们时以及在将它们交给任何请求它们之前调用Cursor#setNotificationUri
。然后在Cursor
的Uri名称空间发生更改时调用ContentResolver#notifyChange
。
例如,假设您正在创建一个简单的邮件应用程序,并且您希望在新邮件到达时进行更新,并且还提供有关邮件的各种视图。我会定义一些基本的Uri。
content://org.example/all_mail
content://org.example/labels
content://org.example/messages
现在,说我想获得这给了我所有的邮件光标和更新,当新邮件到达时:
Cursor c;
//code to get data
c.setNotificationUri(getContentResolver(), Uri.parse("content://org.example/all_mail");
现在有新邮件到达,所以我通知:
//Do stuff to store in database
getContentResolver().notifyChange(Uri.parse("content://org.example/all_mail", null);
我还应该通知所有Cursor
s选择的标签此新消息符合
for(String label : message.getLabels() {
getContentResolver().notifyChange(Uri.parse("content://org.example/lables/" + label, null);
}
而且也,也许光标观看一个特定的消息,以便通知他们还有:
getContentResolver().notifyChange(Uri.parse("content://org.example/messages/" + message.getMessageId(), null);
的getContentResolver()
通话发生在数据被访问。所以,如果它在Service
或ContentProvider
这是你setNotificationUri
和notifyChange
。您不应该从访问数据的位置这样做,例如Activity
。
AlarmProvider
是一个简单的ContentProvider
,它使用此方法更新Cursor
s。
@randzero意味着你要更新,每当新的项目在数据库中添加的列表视图项目, 这样对吗 ? – 2010-08-23 06:24:05