ste*_*vex 8 android listview cursor
我在ListActivity中有一个ListView绑定到一些数据.我有一个提供数据的内容提供商.
ListActivity通过查询内容解析器来获取数据:
Uri uri = Uri.parse("content://my.provider.DocumentProvider/mystuff");
contentCursor = this.getContentResolver().query(uri, null, null, null, null);
Run Code Online (Sandbox Code Playgroud)
所以现在活动有了光标.它创建一个适配器并将其附加到列表:
ListAdapter adapter = new DocumentListCursorAdapter(this, R.layout.main_row_layout, contentCursor, new String[] { "titleColumn" }, new int[] { titleColumnIndex });
setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
这很好用; 列表显示光标中的数据.
但现在内容提供商有了新的数据.我希望列表更新以显示新数据.
我见过的例子涉及对适配器的notifyDataSetChanged的调用,但在我看来,这打破了内容提供者和列表之间的分离,这就是消费内容.
内容提供者是否需要知道哪些适配器附加到游标,以便它可以调用其notifyDataSetChanged方法?或者是否有更好的方式,不会看到这两种方式耦合这种方式.
我在这里找到了答案:
http://mylifewithandroid.blogspot.com/2008/03/observing-content.html
简而言之,提供程序调用notifyChange来指示URI上的内容已更改:
getContext().getContentResolver().notifyChange(uri, null);
Run Code Online (Sandbox Code Playgroud)
ListActivity调用游标上的setNotificationUri来注册它有兴趣接收更改通知:
contentCursor.setNotificationUri(getContentResolver(), uri);
Run Code Online (Sandbox Code Playgroud)
(感谢njzk2指出我正确的方向).
| 归档时间: |
|
| 查看次数: |
5239 次 |
| 最近记录: |