SQLite Database “context” passed to adapter

自古美人都是妖i 提交于 2019-11-30 23:52:56

问题


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 wich in this example stands for context.

Now I don't know what to pass in the adapter as context, since im not calling adapter from activity.

Can someone please explain this?


回答1:


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




回答2:


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).

 public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       Context contextNew = this;
       myClass(contextNew);

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):

import android.app.Service;
import android.content.Context;

public class BatchUploadGpsData extends Service {
    public Context contextNew;

    @Override
    public void onCreate() {
        contextNew = getApplicationContext();

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.




回答3:


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.



来源:https://stackoverflow.com/questions/5879957/sqlite-database-context-passed-to-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!