SQLite 数据库“上下文”传递给适配器
我已按照本教程在我的Android应用程序中使用SQLite db。 由于我是初学者,因此我在理解示例中使用的“上下文”参数时遇到问题。
我想调用适配器并从不扩展活动的类中插入/更新/删除记录,在本例中代表上下文。
现在我不知道要在适配器中传递什么作为上下文,因为我没有从活动中调用适配器。
有人可以解释一下吗?
I have followed this tutorial to use SQLite db in my android app.
Since I am a beginner I'm having problems understanding "context" parameter used in the example.
I want to call adapter and insert/update/delete records from a class that does not extend activity which in this example stands for context.
Now I don't know what to pass in the adapter as context, since I'm not calling adapter from activity.
Can someone please explain this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 ActivityName.this 作为类上下文作为参数传递给适配器类的构造函数
ActivityName 是您在其中调用适配器的 Activity 类的名称
Pass the ActivityName.this as class context as argument to the adapter class's constructor
the ActivityName is the name of the Activityclass in which you are calling the adapter
你可以想象上下文定义了 sqlite 数据库存在的位置/时间。 sqlite 数据库本身并不存在,它们存在于您的活动的范围内,因此存在于您的活动的上下文中。
对于接下来的步骤,您必须了解上下文是一个动态的“事物”(在现实生活中,您可以将其想象为某人此时此地)。环境因活动及其时刻而异,就像你的此时此地是你的,并且只属于你,并随着时间的推移而变化。
如果您从活动中调用一个类,那么这就可以了(从活动本身内部传递活动的上下文是可以的 - 有点像您对您的好友说:这就是我现在的感受)。
一个简单的解决方案(灵丹妙药)是在 MyActivity 中创建一个静态上下文变量(contextVariable),并通过以下方式直接访问它:MyActivity.contextVariable。但这不起作用,因为当您尝试声明/使用静态上下文变量时会收到错误。
因此,如果您计划在不是从主活动中调用的服务中使用 sqlite,例如由广播接收器触发的服务(服务和接收器本身都没有上下文),那么您必须从所述接收器内访问原始应用程序的上下文。
访问原始活动的上下文很简单,但远非显而易见。
这对我有用(感谢 @ZiGi 和 @Cristian):
这是工作代码的示例,每次 Android 设备连接到 WIFI 网络时,该代码都会将导航数据上传到网络上的数据库。我有一个接收器监听连接变化(作为一个单独的类存在,直接从“清单文件”调用)。
我希望这是有道理的,如果您想了解更多详细信息,请查看 我的这篇文章,其中我有所述接收器的完整(准系统)代码。
you could imagine that the context defines WHERE/WHEN the sqlite database exists. sqlite databases do not exist on their own, they exist within the confines of your activity, thus in your activity's context.
for the next steps you must understand that the context is a dynamic "thing" (in reallife you could imagine it as someone's HERE and NOW). the context is individual to the activity and its moment, just as your here and now are yours and yours only and change over time.
if you are calling a class from within your activity, then this does the trick (passing the activity's context from within the activity itself is OK - sorta like you saying to your buddy: this is how i am feeling NOW).
an easy all around solution (panacea) would be to create a static Context variable (contextVariable) inside MyActivity and access it directly from without via: MyActivity.contextVariable. but that does not work, because you get an error when you try to declare/use a static Context variable.
So, if you plan on using sqlite inside a service that is NOT called from within the main activity, like, for example, a service triggered by a broadcast receiver (neither a service nor a receiver have a context per se), then you must access the original application's context from within said receiver.
accessing the original activity's context is simple, but far from obvious.
this works for me (thanx @ZiGi and @Cristian):
this is an example from working code that uploads navigation data to a database on the web every time the android device connects to a WIFI network. i have a receiver listening to connectivity changes (existing as a separate class called directly "from" Manifest file).
i hope that this makes sense, if you want more detail on this, check out this post of mine where i have the complete (barebones) code for said receiver.
正如您在示例中看到的,有一个上下文传递给 ToDoAdapter。您可以将活动作为上下文或activity.getApplicationContext() 传递。 此处了解上下文。
As you see in the example, there is a context passed to the ToDoAdapter. You can pass activity as a context or activity.getApplicationContext(). Read about context here.