在自定义 SipmleCursorAdapter 中管理 DbAdapter
我有类 MyCustomAdapter
public class MyCustomAdapter extends SimpleCursorAdapter{
}
我有其他类称为 DbAdapter,其中有带有 sql 查询的函数。我想访问 MyCustomAdapter 类中的函数
DbAdapter db and next I have to add
db = new DbAdapter(this)
,但它不起作用。我尝试过
Contex contex
db = new DbAdapter(contex) but then I have java.lang.NullPointerException
有什么方法可以在 MyCustomAdapter 中访问我的 DbAdapter 吗?
I have class MyCustomAdapter
public class MyCustomAdapter extends SimpleCursorAdapter{
}
I have other class called DbAdapter, where I have functions with sql query. I'd like to access functions from class MyCustomAdapter
DbAdapter db and next I have to add
db = new DbAdapter(this)
but it doesnt work. I tried
Contex contex
db = new DbAdapter(contex) but then I have java.lang.NullPointerException
Is any way to access my DbAdapter within MyCustomAdapter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让您的
MyCustomAdapter
类接受Context
对象作为参数:在调用活动中,您只需为上下文传递
this
即可。您收到此错误是因为 DVM 尝试调用不存在的
SimpleCursorAdapter(Context)
构造函数。问题的解决方案是使用
super(...)
显式调用SimpleCursorAdapter
的有效构造函数,或者使MyCursorAdapter
构造函数接受与 SimpleCursorAdapter 构造函数之一的参数相同。走哪条路取决于您想用游标适配器做什么。如果某些构造函数参数有常量值,则可以使用它们,如果没有,则需要将它们包含在
MyCursorAdapter
构造函数中。例如,假设您没有任何构造函数参数的常量值:
可能会找到有关此内容的更多信息 此处。
Make your
MyCustomAdapter
-class accept aContext
-object as a parameter:In the calling activity, you can then simply pass
this
for the context.You're getting this error because the DVM tries to invoke a
SimpleCursorAdapter(Context)
-constructor, which does not exist.The solution to your problem is either explicitly calling a valid constructor of
SimpleCursorAdapter
by usingsuper(...)
or making theMyCursorAdapter
-constructor accept the same parameters as one of theSimpleCursorAdapter
-constructors.What way to go relies on what you want to do with your Cursor-adapter. If you have constant values for certain constructor-arguments, you can use those, if you don't, you'll need to include them in your
MyCursorAdapter
-constructor.As an example, this assumes you don't have constant values for any constructor-argument:
Some further information on this might be found here.