Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get this Context in a static method

Tags:

android

I have a problem in initializing a new object using this in a static method. I have a database class like follow.

public class LatLogDBAdapter {   
 private final Context mCtx;     
 private DatabaseHelper mDbHelper;
 private SQLiteDatabase mDb;

 private static class DatabaseHelper extends SQLiteOpenHelper {

      DatabaseHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }


      @Override
      public void onCreate(SQLiteDatabase db) {
       Log.w(TAG, DATABASE_CREATE);
       db.execSQL(DATABASE_CREATE);
      }      
     }

I have a static method in a another class, in that static method I like to initialize DatabaseHelper using this Context, but the error is "Can't use this in a static context".

My static method in a separate class is as follow,

 public class DetailMapView extends FragmentActivity {
            public static void updateLocation(String number, String LatLong){
                 LatLogDBAdapter dbHelper = new LatLogDBAdapter(this);

           }

    }

How can I do it not to have error of "Can't use this in a static context". Thanks

like image 889
Bryanyan Avatar asked Apr 06 '13 01:04

Bryanyan


1 Answers

public class DetailMapView extends FragmentActivity {
        public static void updateLocation(Context context, String number, String LatLong){
             LatLogDBAdapter dbHelper = new LatLogDBAdapter(context);

       }

}

Add a Context as a parameter to your static method, and pass it in when you call the method.

like image 115
edthethird Avatar answered Nov 19 '22 23:11

edthethird