如何在Cursorloader中关闭数据库
我目前正在研究 Fragments 中的 ListViews。列表视图由 Cursorloader 加载,但没有 ContentManager。所以代码看起来像这样并且它可以工作:
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Log.d("SoapERP", "onCreateLoader");
CursorLoader loader = new CursorLoader(getActivity()) {
final DBHelper dbhelper1= new DBHelper(getActivity());
@Override
public Cursor loadInBackground() {
Cursor c = null;
dbhelper1.open();
c = dbhelper1.fetchAllMatnameswithID();
// dbhelper1.close();
return c;
}
};
return loader;
我的问题是我收到一条 LogCat 错误消息,表明数据库未关闭。但如果我使用 dbhelper.close();我收到错误“数据库已关闭”,这也是可以理解的,因为它就在 return 语句之前。在 return 语句代码不可访问之后,如果我声明 DBHelper dbhelper1 Final,则程序崩溃,logcat 中没有任何信息。那我的失败是什么???
I'm currently working on ListViews in Fragments. The Listviews are loaded by Cursorloader, but without ContentManager. So the code looks like this and it works:
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Log.d("SoapERP", "onCreateLoader");
CursorLoader loader = new CursorLoader(getActivity()) {
final DBHelper dbhelper1= new DBHelper(getActivity());
@Override
public Cursor loadInBackground() {
Cursor c = null;
dbhelper1.open();
c = dbhelper1.fetchAllMatnameswithID();
// dbhelper1.close();
return c;
}
};
return loader;
My problem is that I get an LogCat-Error-Message that the database wasn't closed. But if I use dbhelper.close(); I get the Error "Database is already closed" wich is also understandable because it is just before the return statement. After the return statement code is not reachable and if I declare DBHelper dbhelper1 final the program crashes without any information in logcat. So what is my fail???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我在此处找到了 Dianne Hackborn 的正确说法android框架开发:“内容提供者在创建其托管进程时创建,并且只要该进程存在就一直存在,因此不需要关闭数据库——它将作为内核的一部分被关闭Dianne Hackborn Android 框架工程师 [email protected] “ - 所以让我们使用内容提供程序。
Finally I found here the right statement as of Dianne Hackborn from android framework development: "A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database -- it will get closed as part of the kernel cleaning up the process's resources when the process is killed. Dianne Hackborn Android framework engineer [email protected] " - so let's use Content Provider.