SQLite Database “context” passed to adapter

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.

  • Submitting apps to Android Market and Amazon Appstore
  • aapt not found under the right path
  • What is the correct way to deal with the deprecated Fragment events onAttach and onInflate
  • How to show a dialog to confirm that the user wishes to exit an Android Activity?
  • Android LicenseChecker crashes with null pointer exception
  • Android: PNG Transparency Failing with BitmapFactory.decodeStream(…) and Assets Folder
  • 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?

    Related posts:

    No resource identifier found for attribute fontFamily in package android
    Default keystore file does not exist?
    Android M : Billing and GCM permissions
    Finite State Machine (FSM) and Android's Java
    ActionBarSherlock Side Menu Navigation Drawer
    BadParcelableException - BroadcastReceiver intent crash
  • Maintaining a PhoneGap Hybrid/Web App Codebase
  • Android - Acceleration down (smash)
  • Android preferences onclick event
  • Can't register receiver dynamically
  • Can I find the MAC address of my Access point in Android?
  • nullpointer exception in oncreateView() of fragment
  • 3 Solutions collect form web for “SQLite Database “context” passed to adapter”

    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

    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.

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.