在自定义 SipmleCursorAdapter 中管理 DbAdapter

发布于 2024-12-15 04:42:40 字数 475 浏览 1 评论 0原文

我有类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-12-22 04:42:40

让您的 MyCustomAdapter 类接受 Context 对象作为参数:

public class MyCustomAdapter extends SimpleCursorAdapter{

  private final Context context;

  public MyCustomAdapter(Context c){
   this.context = c;
  }

}

在调用活动中,您只需为上下文传递 this 即可。


您收到此错误是因为 DVM 尝试调用不存在的 SimpleCursorAdapter(Context) 构造函数。

问题的解决方案是使用 super(...) 显式调用 SimpleCursorAdapter 的有效构造函数,或者使 MyCursorAdapter 构造函数接受与 SimpleCursorAdapter 构造函数之一的参数相同。

走哪条路取决于您想用游标适配器做什么。如果某些构造函数参数有常量值,则可以使用它们,如果没有,则需要将它们包含在 MyCursorAdapter 构造函数中。

例如,假设您没有任何构造函数参数的常量值:

public class MyCustomAdapter extends SimpleCursorAdapter{

  private final Context context;

  public MyCustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to){
   super(Context context, int layout, Cursor c, String[] from, int[] to); // Call the super-
                                                                        // classes constructor
   this.context = context; // Save the context for further use
  }

}

可能会找到有关此内容的更多信息 此处

Make your MyCustomAdapter-class accept a Context-object as a parameter:

public class MyCustomAdapter extends SimpleCursorAdapter{

  private final Context context;

  public MyCustomAdapter(Context c){
   this.context = c;
  }

}

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 using super(...) or making the MyCursorAdapter-constructor accept the same parameters as one of the SimpleCursorAdapter-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:

public class MyCustomAdapter extends SimpleCursorAdapter{

  private final Context context;

  public MyCustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to){
   super(Context context, int layout, Cursor c, String[] from, int[] to); // Call the super-
                                                                        // classes constructor
   this.context = context; // Save the context for further use
  }

}

Some further information on this might be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文