何时在Android中关闭游标? [英] When to close cursor in Android?
问题描述
我有一个应用程序,该应用程序使用游标通过 rawQuery
从SQLite数据库中选择数据,以填充Android中的ListView。每次用户单击列表视图项时,我都会创建一个新的Activity实例来重新填充列表视图。最好调用 cursor.close()
和 db.close()
以避免内存问题吗?我的活动实际上在 OnDestroy()
中有 db.close()
。
一旦在方法中检索到该特定对象的值,就可以关闭光标
。 / p>
btw ...您不必每次为用户点击事件重新创建 listview
。只需通知您,适配器的数据已在列表视图中设置。
类似
youradaptername.notifyDataSetChanged();
这应该自动填充ur listview
中的内容
I have an app that uses a cursor to select data via rawQuery
from an SQLite DB to populate a ListView in Android. Every time the user clicks on a listview item I create a new instance of Activity to re-populate listview. Is it better to call cursor.close()
and db.close()
to avoid memory problems? I actually have db.close()
in OnDestroy()
of my activity.
You can close the cursor
once you have retrieved the values for that particular object inside your method.
btw...You don't have to recreate a listview
every time for a user click event. Just notify that there is some change in data of your adapter that has been set on the listview.
Something like
youradaptername.notifyDataSetChanged();
This should repopulate contents inside ur listview
automatically.
这篇关于何时在Android中关闭游标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!