文件管理器源码分析(三)

//favorite数据数据
//SQLiteOpenHelper助管理数据和版本工具
//过继载方法快速实现了我自己FavoriteCRUD
//FileOperationHelper仍然CRUD只不1个是数据1个是文件的
public class FavoriteDatabaseHelper extends SQLiteOpenHelper {
    //下面6字段是数据的名字和版本号、表的名字和3字段
 private final static String DATABASE_NAME = "file_explorer";
 private final static int DATABASE_VERSION = 1;
 private final static String TABLE_NAME = "favorite";
 public final static String FIELD_ID = "_id";
 public final static String FIELD_TITLE = "title";
 public final static String FIELD_LOCATION = "location";
 private boolean firstCreate;
 //数据会通知监听器
 private FavoriteDatabaseListener mListener;
 private static FavoriteDatabaseHelper instance;
 public interface FavoriteDatabaseListener {
        void onFavoriteDatabaseChanged();
 }
    //这个方法下面的态获的方法太和谐啊~
 //乍一看以为是例模呢,实则不是~
 public FavoriteDatabaseHelper(Context context, FavoriteDatabaseListener listener) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
 instance = this;
 mListener = listener;
 }
    //这个觉只是方便存储了个类的能保这个类只有1
 public static FavoriteDatabaseHelper getInstance() {
        return instance;
 }
    //数据库创1sql
 @Override
 public void onCreate(SQLiteDatabase db) {
        String sql = "Create table " + TABLE_NAME + "(" + FIELD_ID + " integer primary key autoincrement,"
 + FIELD_TITLE + " text, " + FIELD_LOCATION + " text );";
 db.execSQL(sql);
 firstCreate = true;
 }
    //级的直接的数据库,如果存在
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
 db.execSQL(sql);
 onCreate(db);
 }
    //1
 public boolean isFirstCreate() {
        return firstCreate;
 }
    //判断1个文件路径是存在或者Favorite文件
 public boolean isFavorite(String path) {
        String selection = FIELD_LOCATION + "=?";
 String[] selectionArgs = new String[] {
            path
        };
 SQLiteDatabase db = this.getReadableDatabase();
 Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, null, null, null);
 if (cursor == null)
            return false;
 boolean ret = cursor.getCount() > 0;
 cursor.close();
 return ret;
 }
    //Favorite
 public Cursor query() {
        SQLiteDatabase db = this.getReadableDatabase();
 Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
 return cursor;
 }
    //
 public long insert(String title, String location) {
        if (isFavorite(location))
            return -1;
 SQLiteDatabase db = this.getWritableDatabase();
 long ret = db.insert(TABLE_NAME, null, createValues(title, location));
 mListener.onFavoriteDatabaseChanged();
 return ret;
 }
    //根据id,删除一。如果需要然后通知相监听器
 public void delete(long id, boolean notify) {
        SQLiteDatabase db = this.getWritableDatabase();
 String where = FIELD_ID + "=?";
 String[] whereValue = {
            Long.toString(id)
        };
 db.delete(TABLE_NAME, where, whereValue);
 if (notify)
            mListener.onFavoriteDatabaseChanged();
 }
    //根据1通知相监听器
 public void delete(String location) {
        SQLiteDatabase db = this.getWritableDatabase();
 String where = FIELD_LOCATION + "=?";
 String[] whereValue = {
            location
        };
 db.delete(TABLE_NAME, where, whereValue);
 mListener.onFavoriteDatabaseChanged();
 }
    //更新1
 public void update(int id, String title, String location) {
        SQLiteDatabase db = this.getWritableDatabase();
 String where = FIELD_ID + "=?";
 String[] whereValue = {
            Integer.toString(id)
        };
 db.update(TABLE_NAME, createValues(title, location), where, whereValue);
 mListener.onFavoriteDatabaseChanged();
 }
    private ContentValues createValues(String title, String location) {
        ContentValues cv = new ContentValues();
 cv.put(FIELD_TITLE, title);
 cv.put(FIELD_LOCATION, location);
 return cv;
 }
}


你可能感兴趣的:(源码,文件管理器)