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.

  • Android Save object as blob in sqlite
  • Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?
  • Android In App Billing: Can't start launchPurchaseFlow because launchPurchaseFlow is in progress
  • How to send email with attachment using GmailSender in android
  • What's different between using WebView and Browser for YouTube Video api?
  • Android - problems animating ActionBar icon from Fragments
  • 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:

    Android GridView draw dividers
    Is android:exported=“true” really necessary for an authentication service?
    How to detect a click on a polyline
    Set TextView style (bold or italic)
    Items in ActionBarCompat are showed always in Overflow
    Using version 1.0.0-alpha7 of the constraint library, which is obsolete
  • Fixed and always visible footer below ListFragment
  • How to draw with a default Android font on a Canvas?
  • Disable WebView messages from logcat output
  • Best way to update data with a RecyclerView adapter
  • Google plus for android: Invalid Credentials
  • Creating background services for Bluetooth low energy on Android
  • 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.