Android 如果SimpleCursorAdapter关闭,则ListView为空()

Android 如果SimpleCursorAdapter关闭,则ListView为空(),android,android-listview,simplecursoradapter,Android,Android Listview,Simplecursoradapter,当我调用游标对象上的close()方法时,我无法理解为什么我的ListActivity及其对应的listView为空 让我解释一下我自己 我从数据库中检索一些值,并在游标对象中获得结果。之后,我创建SimpleCursorAdapter,并将db中的列与listView中的字段绑定 很有魅力,但是 如果我在onCreate()方法的末尾调用cursor.close(),我的listView会显示为空吗 如果我将值从cursor记录到LogCat,它们一直存在,直到调用cursor.close()

当我调用游标对象上的close()方法时,我无法理解为什么我的ListActivity及其对应的listView为空

让我解释一下我自己

我从数据库中检索一些值,并在游标对象中获得结果。之后,我创建SimpleCursorAdapter,并将db中的列与listView中的字段绑定

很有魅力,但是

如果我在onCreate()方法的末尾调用cursor.close(),我的listView会显示为空吗

如果我将值从cursor记录到LogCat,它们一直存在,直到调用cursor.close()为止,这很有意义,但是为什么调用cursor.close()时listAdapter是空的??? 我希望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中,我不必使用这个.cursor.getCount()从中获取引用/值。只需说cursor.getCount()这是怎么允许的?

如果您的活动被破坏(即在
ondestory()
中),您就不应该关闭光标,因为CursorAdapter的实现希望它被打开,以便能够重新查询和过滤它

由于您仍在调用
startManagingCursor
,您的活动将在相应的活动生命周期事件中自动取消激活、重新查询并关闭光标,因此无需自行关闭

为什么会这样

适配器
需要提供的
光标
中的数据,并相应地准备
列表视图
,当您调用
光标时,关闭()
光标被释放并无效。(表示没有数据。)

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

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
当您准备离开
活动
并从
活动
返回时,必须关闭
光标
,否则
光标
因该
活动
而泄漏

  • 来自
  • 有什么原因吗
只要说cursor.getCount()就足够了,为什么允许这样做

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

  • 欲了解更多信息,请参阅