public class DBHelper extends SQLiteOpenHelper {
private static final String TAG = "DBHelper";
private static final int DB_VERSION = 1;
private Context context;
private String DB_PATH;
private String DB_NAME;
private String TABLE_NAME;
private boolean booCreate;
private String createString;
private SQLiteDatabase myDataBase;
public DBHelper(Context context, String db_name, String table_name,
boolean booCreate, String createString) {
super(context, db_name, null, DB_VERSION);
this.context = context;
this.DB_NAME = db_name;
this.DB_PATH = "/data/data/" + FindPackageName() + "/databases/";
Log.d(TAG, "DBPATH=" + DB_PATH);
this.TABLE_NAME = table_name;
this.createString = createString;
this.booCreate = booCreate;
Log.d(TAG,"booCreate=" + String.valueOf(this.booCreate));
try {
createDataBase();
} catch (IOException e) {
throw new Error("Unable to Create Database");
}
}
,並在您的活動
dbHelper = new DBHelper(this, this.getString(R.string.db_name_contact),
this.getString(R.string.table_name_contacts), true, createString);
關注此tutorial
http://stackoverflow.com/questions/9725394/android-context-outside-of-activity-class/9725458#9725458 – 2012-04-12 20:45:18
我試過,我得到一個「沒有封閉的類型活動的實例是可訪問的範圍「 – arielschon12 2012-04-12 20:51:05
IMO,您應該始終將應用程序,而不是活動,上下文傳遞給這些類型的東西。 – Phix 2012-04-12 20:55:42