我在ListActivity中有一个绑定到某些数据的ListView。我有一个提供数据的内容提供者。Android ListView实时更新
的ListActivity通过查询内容解析器得到数据:
Uri uri = Uri.parse("content://my.provider.DocumentProvider/mystuff");
contentCursor = this.getContentResolver().query(uri, null, null, null, null);
所以,现在的活动有光标。它创建一个适配器,并将其附加到列表中:
ListAdapter adapter = new DocumentListCursorAdapter(this, R.layout.main_row_layout, contentCursor, new String[] { "titleColumn" }, new int[] { titleColumnIndex });
setListAdapter(adapter);
This works fine;该列表显示光标中的数据。
但现在内容提供商有新的数据。我想要更新列表以显示新数据。
我见过的例子涉及调用适配器的notifyDataSetChanged,但在我看来,这打破了内容提供者和消耗内容的列表之间的分离。
内容提供者是否需要知道哪些适配器连接到了光标,以便它可以调用它们的notifyDataSetChanged方法?或者有没有更好的方式,没有看到这两种方式耦合这种方式。
你看过http://developer.android.com/reference/android/content/ContentResolver.html#notifyChange%28android.net.Uri,%20android.database.ContentObserver,%20boolean%29? – Luksprog 2012-03-09 14:29:38
我曾尝试使用registerContentObserver进行注册,但在光标更改时未看到更改。应该这样做吗? – stevex 2012-03-09 14:42:23
与slukian相同,再加上你可能需要'contentCursor.setNotificationUri(cr,uri);' – njzk2 2012-03-09 14:44:41