如果 SimpleCursorAdapter 关闭,ListView 为空()
我无法理解为什么当我在 cursor 对象上调用 close() 方法时,我的 ListActivity 及其相应的 listView 为空?
让我解释一下......
我从数据库中检索一些值并在我的游标对象中获取结果。之后,我创建了 SimpleCursorAdapter 并将数据库中的列与 listView 中的字段绑定。
工作起来就像一个魅力,但是......
如果我在 onCreate() 方法末尾调用 cursor.close() ,我的 listView 显示为空?
如果我将游标中的值记录到 LogCat,它们会一直存在,直到调用cursor.close(),这是完全有道理的,但为什么当cursor.close()时listAdapter会被清空em> 被称为??? 我希望 ListAdapter listAdapter = new SimpleCursorAdapter(...) 保存“绑定”到 listView 的值,直到我们的活动被销毁...
为什么这是所以?何时以及为什么需要关闭游标?
public class ListTrainingsView extends ListActivity{
private SQLiteDatabase db;
private ListAdapter listAdapter;
private Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_trainings);
DatabaseHelper helper = new DatabaseHelper(this);
this.db = helper.getWritableDatabase();
this.cursor = db.query("trainings", fields, null, null, null, null, null);
startManagingCursor(cursor);
this.listAdapter = new SimpleCursorAdapter(this,
R.layout.list_trainings_item,
cursor,
new String[] {"name"},
new int[] { R.id.name_entry}
);
this.setListAdapter(this.listAdapter);
//If I call
//cursor.close();
//shown list is empty
另一个问题是更多基本 Java 语言类型的问题...我来自 PHP OO 背景,如果您定义成员变量,则必须使用语法 $this->cursor。我注意到,在 Android/Java 中,我不必使用 this.cursor.getCount() 来从中获取引用/值。说cursor.getCount()就足够了,为什么这是允许的?
I have a problem understanding why is my ListActivity and its corresponding listView empty when I call close() method on cursor object?
Let me explain myself a bit...
I retreive some values from a DB and get the result in my cursor object. After that I create my SimpleCursorAdapter and bind column from db with a field in my listView.
Works like a charm, but...
If I call cursor.close() at the end of onCreate() method my listView is shown empty?
If I log the values from cursor to LogCat they're there until calling cursor.close() and that makes perfect sense, but why is the listAdapter emptied when cursor.close() is called???
I would expect ListAdapter listAdapter = new SimpleCursorAdapter(...) to hold the values that are "binded" to a listView until we activity is destroyed...
Why is this so? When and why is necessary to close cursor?
public class ListTrainingsView extends ListActivity{
private SQLiteDatabase db;
private ListAdapter listAdapter;
private Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_trainings);
DatabaseHelper helper = new DatabaseHelper(this);
this.db = helper.getWritableDatabase();
this.cursor = db.query("trainings", fields, null, null, null, null, null);
startManagingCursor(cursor);
this.listAdapter = new SimpleCursorAdapter(this,
R.layout.list_trainings_item,
cursor,
new String[] {"name"},
new int[] { R.id.name_entry}
);
this.setListAdapter(this.listAdapter);
//If I call
//cursor.close();
//shown list is empty
Another question is more of basic Java language type of question... I come from PHP OO background and there if you define member variable you have to work with it in object methods using syntax $this->cursor. I've noticed that in Android/Java I don't have to use this.cursor.getCount() to get the reference/value from it. It's enough to say cursor.getCount() How come this is allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 Activity 被销毁之前(即在
onDestroy()
中),您不应该关闭游标,因为 CursorAdapter 的实现希望它打开以便能够重新查询和过滤它。由于您无论如何都会调用
startManagingCursor
,因此您的 Activity 将在适当的 Activity 生命周期事件发生时自动停用、重新查询并关闭游标,因此无需自行关闭它。You should not close your cursor unil your Activity is destroyed (i.e. in
onDestroy()
) because the implementation of CursorAdapter expects it to be open to be able to requery and filter it.Since you're calling
startManagingCursor
anyway, your Activity will automatically deactivite, requery and close the cursor upon the appropriate Activity lifecycle events, so there is no need to close it yourself.Adapter
需要来自提供的Cursor
的数据并相应地准备ListView
,当您调用cursor.close()
时,< code>Cursor 被释放并变得无效。 (表示没有数据。)当您要离开
Activity
并从Activity
返回时,必须关闭Cursor
,否则Cursor
> 该活动
被泄露。Java 的结构基于类。每个动作都是在特定的类中完成的。即使回显“单行”也需要一个类。当您在课堂上时,您可以使用
this
OR 直接访问类成员的变量Adapter
needs data from providedCursor
and prepareListView
accordingly, and when you callcursor.close()
theCursor
is released and made invalid. (means there is no data.)It is necessary to close the
Cursor
while you are about to leave theActivity
and going back from theActivity
otherwiseCursor
got leaked for thatActivity
.The structure of Java is based on classes. Every action is done within a specific class. Even echoing "single line" would need a class. and when you are in a class you can access class members with
this
OR with direct their variables