android – 如果SimpleCursorAdapter关闭,ListView为空()

android – 如果SimpleCursorAdapter关闭,ListView为空(),第1张

概述我有一个问题,当我在游标对象上调用close()方法时,为什么我的ListActivity及其相应的listView为空? 让我解释一下…… 我从数据库中检索了一些值,并在我的光标对象中获取结果.之后,我创建了我的SimpleCursorAdapter并使用listView中的字段从db绑定列. 像魅力一样,但…… 如果我在onCreate()方法的末尾调用cursor.close(),我的lis 我有一个问题,当我在游标对象上调用close()方法时,为什么我的ListActivity及其相应的ListVIEw为空?

让我解释一下……

我从数据库中检索了一些值,并在我的光标对象中获取结果.之后,我创建了我的SimpleCursorAdapter并使用ListVIEw中的字段从db绑定列.

像魅力一样,但……

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

如果我将值从光标记录到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;@OverrIDepublic 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);    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()为什么这是允许的?

解决方法

Why is this so?

适配器需要来自提供的Cursor的数据并相应地准备ListVIEw,当你调用cursor.close()时,Cursor被释放并变为无效. (表示没有数据.)

When and why is necessary to close cursor?

当您即将离开活动并从活动返回时,有必要关闭光标,否则Cursor被泄露为该活动.

>参考文献来自documentation
>为什么它是necessary

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

Java的结构基于类.每个动作都在特定的课程中完成.即使回应“单行”也需要一个班级.当你在一个班级时,你可以通过这个OR直接访问他们的变量

>欲了解更多,请参阅here

总结

以上是内存溢出为你收集整理的android – 如果SimpleCursorAdapter关闭,ListView为空()全部内容,希望文章能够帮你解决android – 如果SimpleCursorAdapter关闭,ListView为空()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1126955.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存