每当数据发生变化时就创建新的 SimpleCursorAdapter 吗?
我刚刚开始进行 Android 开发,一直在构建一个使用 ListActivity
、SQLiteDatabase
和 SimpleCursorAdapter
的简单应用程序。
Android 开发者网站上有一个示例项目,演示了 SimpleCursorAdadpter
的用法。查看实现,每当由于某些用户操作而修改底层数据库时,ListActivity
都会显式调用以下函数来“刷新”列表:
private void fillData() {
// Get all of the rows from the database and create the item list
mNotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(mNotesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
setListAdapter(notes);
}
它看起来像一个新的 SimpleCursorAdapter< /code> 每次都会创建并通过
setListAdapter()
绑定到视图。这是最好/最干净的实现吗?事实上,这是在 Android 网站上的,这使它具有很大的可信度,但是我查看了 CursorAdapter 文档,发现有一个 changeCursor()
方法似乎可以借用本身比上面的实现更干净,但我只是不确定它会是什么样子。
也许我只是因为什么而烦恼,但来自 C/C++ 世界,每次从数据库中插入/删除一行时看到这个“新”对象创建似乎有点粗暴。
I'm just starting out with Android development and have been building a simple app that uses a ListActivity
, a SQLiteDatabase
, and a SimpleCursorAdapter
.
On the Android developer website there is an example project that demonstrates the usage of the SimpleCursorAdadpter
. Looking at the implementation, whenever the underlying database is modified as a result of some user action, the ListActivity
explicitly calls the following function to "refresh" the list:
private void fillData() {
// Get all of the rows from the database and create the item list
mNotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(mNotesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
setListAdapter(notes);
}
It looks like a new SimpleCursorAdapter
is created every time and bound to the view with setListAdapter()
. Is this the best/cleanest implementation? The fact that this is on the Android website lends it a lot of credibility, however I looked at the CursorAdapter
documentation and saw there is a changeCursor()
method that seems to lend itself to a cleaner implementation than the one above, but I'm just not sure what that might look like.
Maybe I'm just fretting over nothing, but coming from the C/C++ world, and seeing this "new" object created each time you insert/delete a row from the database seems a bit heavy-handed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的你是对的。你可以看到很多次。对于小列表,当列表始终在 onResume 中创建时,可能不会产生任何问题。但这不是好的风格。您可以使用cursor.changeCursor()或adapter.notifyDataSetChanged()。
Yes, you are right. You can see that a lot of times. For small lists that may not produce any problems when list is always created in onResume. But that's no good style. You can use cursor.changeCursor() or adapter.notifyDataSetChanged().