问题描述
我有一个数据库,一个的ListView
和 CustomCursorAdapter
扩展的CursorAdapter
。菜单按钮添加一个项目到数据库中。我想的ListView
更新和显示这种变化。通常情况下它不会显示这个新的项目,直到我去到主屏幕,并重新打开应用程序。
I have a database, a ListView
, and a CustomCursorAdapter
that extends CursorAdapter
. A menu button adds an item to the database. I want the ListView
to update and show this change. Normally it doesn't show this new item until i go to the homescreen and reopen the application.
我最终获得通过调用 cursor.requery()
或 mCustomCursorAdapter.changeCursor(newCursor)
工作每当我增加了一个新的项目,但是当我在的CursorAdapter
构造函数中设置autoRequery为false,它的工作一样。为什么它正确更新时autoRequery设置为false?
I did eventually get it to work by calling cursor.requery()
or mCustomCursorAdapter.changeCursor(newCursor)
whenever I added a new item, but when I set autoRequery to false in the CursorAdapter
constructor, it worked just the same. Why does it update correctly when autoRequery is set to false?
我使用的CursorAdapter
是否正确?什么是保持更新的数据库中的表的标准方法?又是什么autoRequery办?
Am I using CursorAdapter
correctly? What is the standard way of keeping the list updated with the database? And what does autoRequery do?
推荐答案
惯用恕我直言正确的方式来自动更新光标
s是叫<$c$c>Cursor#setNotificationUri$c$c>在创建时,他们被移交给任何要求之前。然后调用<$c$c>ContentResolver#notifyChange$c$c>当在光标什么
的开放的我们的命名空间的变化。
The idiomatic and imho correct way to automatically update Cursor
s is to call Cursor#setNotificationUri
when they are created and before they are handed off to whatever requested them. Then call ContentResolver#notifyChange
when anything in that Cursor
's Uri's namespace changes.
例如,假设您正在创建一个简单的邮件应用程序,你想,当新邮件到达更新,还提供了对邮件的各种意见。我有一些基本的URI定义的。
For example, suppose you were creating a simple mail application and you wanted to update when new mail arrived but also provide various views on the mail. I'd have some basic Uri's defined.
content://org.example/all_mail
content://org.example/labels
content://org.example/messages
现在,说我希望得到一个游标这给了我所有的邮件和更新,当新邮件到达时:
Now, say I wanted to get a cursor that gave me all mail and be updated when new mail arrives:
Cursor c;
//code to get data
c.setNotificationUri(getContentResolver(), Uri.parse("content://org.example/all_mail");
现在新邮件到达,所以我通知:
Now new mail arrives so I notify:
//Do stuff to store in database
getContentResolver().notifyChange(Uri.parse("content://org.example/all_mail", null);
我也应该通知所有的光标
s表示选择标签这个新的消息见面
I should also notify all the Cursor
s that selected for labels this new message met
for(String label : message.getLabels() {
getContentResolver().notifyChange(Uri.parse("content://org.example/lables/" + label, null);
}
和也,也许光标在查看一个特定的消息,以便通知他们还有:
And also, maybe a cursor is viewing that one specific message so notify them as well:
getContentResolver().notifyChange(Uri.parse("content://org.example/messages/" + message.getMessageId(), null);
在 getContentResolver()
通话发生在这里被访问的数据。所以,如果它在一个服务
或的ContentProvider
这是你 setNotificationUri
和有NotifyChange
。你不应该这样做,从何处访问数据,例如,一个活动
。
The getContentResolver()
calls happen where the data is accessed. So if it's in a Service
or ContentProvider
that is where you setNotificationUri
and notifyChange
. You should not be doing that from where the data is accessed, e.g., an Activity
.
<一个href="http://android.git.kernel.org/?p=platform/packages/apps/AlarmClock.git;a=blob;f=src/com/android/alarmclock/AlarmProvider.java;h=5849a384f0b94259bc6713b9f11cf1c9749ed4ca;hb=062d863a156a3564a839ab5a718f31500298635f"><$c$c>AlarmProvider$c$c>是一个简单的的ContentProvider
使用此方法来更新光标
秒。
AlarmProvider
is a simple ContentProvider
that uses this method to update Cursor
s.
这篇关于Android的:如何使用的CursorAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!