如果 SimpleCursorAdapter 关闭,ListView 为空()

发布于 2024-12-11 17:28:34 字数 1632 浏览 0 评论 0原文

我无法理解为什么当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小情绪 2024-12-18 17:28:34

在您的 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.

风流物 2024-12-18 17:28:34

为什么会这样?

Adapter 需要来自提供的 Cursor 的数据并相应地准备 ListView,当您调用 cursor.close() 时,< code>Cursor 被释放并变得无效。 (表示没有数据。)

何时以及为什么需要关闭光标?

当您要离开 Activity 并从 Activity 返回时,必须关闭 Cursor,否则 Cursor > 该活动被泄露。

只要说cursor.getCount()就足够了,为什么这是允许的?

Java 的结构基于类。每个动作都是在特定的类中完成的。即使回显“单行”也需要一个类。当您在课堂上时,您可以使用 this OR 直接访问类成员的变量

  • 有关更多信息,请参阅 此处

Why is this so?

Adapter needs data from provided Cursor and prepare ListView accordingly, and when you call cursor.close() the Cursor is released and made invalid. (means there is no data.)

When and why is necessary to close cursor?

It is necessary to close the Cursor while you are about to leave the Activity and going back from the Activity otherwise Cursor got leaked for that Activity.

It's enough to say cursor.getCount() How come this is allowed?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文