何时在Android中关闭游标? [英] When to close cursor in Android?

查看:65
本文介绍了何时在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆