我在ListActivity中有一个绑定(bind)到某些数据的ListView。我有一个提供数据的内容提供商。
ListActivity通过查询内容解析器获取数据:
Uri uri = Uri.parse("content://my.provider.DocumentProvider/mystuff");
contentCursor = this.getContentResolver().query(uri, null, null, null, null);
因此,现在 Activity 有了光标。它创建一个适配器并将其附加到列表:
ListAdapter adapter = new DocumentListCursorAdapter(this, R.layout.main_row_layout, contentCursor, new String[] { "titleColumn" }, new int[] { titleColumnIndex });
setListAdapter(adapter);
这很好用;该列表显示光标中的数据。
但是现在内容提供商有了新数据。我希望列表进行更新以显示新数据。
我看到的示例涉及到对适配器的notifyDataSetChanged的调用,但是在我看来,这打破了内容提供者与使用内容的列表之间的分隔。
内容提供者是否需要知道将哪些适配器附加到游标,以便它可以调用其notifyDataSetChanged方法?还是有一种更好的方法,使这两种方法不相互结合。
最佳答案
我在这里找到了答案:
http://mylifewithandroid.blogspot.com/2008/03/observing-content.html
简而言之,提供程序调用notifyChange来指示URI上的内容已更改:
getContext().getContentResolver().notifyChange(uri, null);
然后ListActivity在光标上调用setNotificationUri,以注册它有兴趣接收更改通知:
contentCursor.setNotificationUri(getContentResolver(), uri);
(感谢njzk2向我指出正确的方向)。