安卓:如何关闭,从类返回到活动光标 [英] Android: How to close a cursor that returns from Class to Activity
问题描述
我有:Accounts.java
I have: Accounts.java
public class Accounts{
private SQLiteDatabase dbConfig;
public Cursor list(Context context, String db, String where, String order) {
DBHelper dbHelper = new DBHelper(context, db);
dbConfig = dbHelper.getReadableDatabase();
Cursor c = dbConfig.query("accounts", new String[]{ "iId","sName"}, where, null, null, null, order);
return c;
}
}
和:MainActivity.java
and: MainActivity.java
Accounts account = new Accounts();
Cursor cursor = account.list(getApplicationContext(), globalDB, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
}
cursor.close();
运行我的应用程序得到一些消息logcat中,如:
Running my application I get some logcat messages like:
close() was never explicitly called on database...
什么是prevent的最佳方式是什么?我怎样才能关闭其他类返回游标?
谢谢
What is the best way to prevent it? How can I close a Cursor that returns from other class? Thanks
推荐答案
在 account.list()
退出,底层的数据库连接仍然是开放的,但目前还没有其余对它的引用。因此,它已经泄露。
After account.list()
exits, the underlying database connection is still open, but there are no remaining references to it. So, it has leaked.
您可以关闭数据库,一旦你完成它,或者你可以通过让你打开一次单一的全球数据库连接,分享所有的活动当中保持打开您的应用程序的生命周期,从不关闭。
You can close the database once you're finished with it, or you can keep it open for the lifetime of your application by having a single global database connection which you open once, share amongst all your activities, and never close.
我推荐后一种方法。它导致更简单code。
I recommend the latter approach. It results in much simpler code.
这篇关于安卓:如何关闭,从类返回到活动光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!