我有使用 SQLite 数据库(PlaceDbProvider)的课程。它是单调的。问题是我有三个使用 PlaceDbProvider 的活动。什么时候为 PlaceDbProvider 调用 destroy 方法更好?我很困惑,因为每个活动都有自己的 onDestroy 方法。
public class PlaceDbProvider {
private static final String DB_NAME = "com.placesmanager";
private static final String TABLE_NAME = "places";
private static final int DB_VESION = 1;
private static final String KEY_ID = "_id";
private static final int ID_COLUMN = 0;
private static final String KEY_NAME = "name";
private static final int NAME_COLUMN = 1;
private Context context;
private Cursor cursor;
private SQLiteDatabase database;
private DbOpenHelper dbOpenHelper;
private static PlaceDbProvider mInstance = null;
private PlaceDbProvider(Context context) {
this.context = context;
init();
}
public static PlaceDbProvider getInstance(Context context) {
if(mInstance == null) {
mInstance = new PlaceDbProvider(context);
}
return mInstance;
}
public int getCount() {
return cursor.getCount();
}
public Place getItem(int position) {
if (cursor.moveToPosition(position)) {
Place placeOnPositon = new Place();
placeOnPositon.setId(cursor.getLong(ID_COLUMN));
placeOnPositon.setName(cursor.getString(NAME_COLUMN));
return placeOnPositon;
} else {
throw new CursorIndexOutOfBoundsException(
"Cant move cursor to postion");
}
}
public long getItemId(int position) {
if (cursor.moveToPosition(position)) {
return cursor.getLong(ID_COLUMN);
} else {
throw new CursorIndexOutOfBoundsException(
"Cant move cursor to postion");
}
}
public long addItem(Place place) {
ContentValues values = new ContentValues();
values.put(KEY_NAME, place.getName());
long id = database.insert(TABLE_NAME, null, values);
refresh();
return id;
}
public boolean removeItem(Place placeToRemove) {
boolean isDeleted = (database.delete(TABLE_NAME, KEY_NAME + "=?",
new String[] { placeToRemove.getName() })) > 0;
refresh();
return isDeleted;
}
public boolean updateItem(long id, String key,String newValue) {
ContentValues values = new ContentValues();
values.put(key, newValue);
boolean isUpdated = (database.update(TABLE_NAME, values, KEY_ID + "=?",
new String[] {id+""})) > 0;
return isUpdated;
}
public void destroy() {
dbOpenHelper.close();
mInstance = null;
}
private void refresh() {
cursor = getAllEntries();
}
public Cursor getAllEntries() {
String[] columnsToTake = { KEY_ID, KEY_NAME, KEY_LAT, KEY_LNG, KEY_TYPE, KEY_INFO, KEY_OWNER};
return database.query(TABLE_NAME, columnsToTake,
null, null, null, null, KEY_ID);
}
private void init() {
dbOpenHelper = new DbOpenHelper(context, DB_NAME, null, DB_VESION);
try {
database = dbOpenHelper.getWritableDatabase();
} catch (SQLException e) {
Log.e(this.toString(), "Error while getting database");
throw new Error("The end");
}
cursor = getAllEntries();
}
//class for creation, opening and db version control
private static class DbOpenHelper extends SQLiteOpenHelper {
public DbOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_DB = "CREATE TABLE " + TABLE_NAME + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL);";
db.execSQL(CREATE_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}