在非上下文类中使用上下文类构造函数
可能的重复:
在android上获取上下文的静态方式?
不确定答案是否是正确,但这件事已经阻止我好几天了。
我有两个类:
- 公共类 CfmDbAdapter,其中包括:
- 私有静态类DatabaseHelper扩展了SQLiteOpenHelper
- 一系列服务数据库的方法
- public class DataToArray 扩展了 Application
我扩展了 Application 只是为了访问 Context,但我必须访问 CfmDbadapter 中的方法。在我所有的 Activity 类中,我都是这样做的:
private CfmDbAdapter db;
db = new CfmDbAdapter(this);
db.open(); //one of the methods I need to access
我不能在 DataToArray 类中这样做,因为它的父类不是 Context 类(我猜)。我也无法静态访问方法,它会破坏整个代码。
Possible Duplicate:
Static Way to get Context on android?
Not sure if the answer is correct, but this thing is stopping me from progress for days already.
I got two classes:
- public class CfmDbAdapter which consist:
- private static class DatabaseHelper extends SQLiteOpenHelper
- series of methods to service the database
- public class DataToArray extends Application
I extended Application just to have access to Context but I have to access methods from CfmDbadapter. In all my Activity classes I do it by:
private CfmDbAdapter db;
db = new CfmDbAdapter(this);
db.open(); //one of the methods I need to access
I can't do that in my DataToArray class since it's parent is not a Context class (I gues). I can't acess methods staticly aswell, it would fck up whole code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看这些链接
到处使用应用程序上下文?
在 Android 上获取“上下文”的静态方法?
Check out these links
Using Application context everywhere?
Static way to get 'Context' on Android?
您可以创建数据库:
a) 最终的
b) 属于该类
或者您可以声明一些其他变量(静态或最终)并在其中放置对 db 的引用。
You can make the db:
a) final
b) belonging to the class
Or you can declare some other variable, (static or final) and put a reference to db in it.