小米开源文件管理器MiCodeFileExplorer-源码研究(7)-Favorite收藏管理和SQLite数据库CRUD
FavoriteDatabaseHelper,存储favorite数据,到SQLite数据库。
SQLiteOpenHelper是一个帮助管理数据库和版本的工具类。
通过继承并重载方法,快速实现了我们自己的Favorite表的CRUD。
怎么感觉和FileOperationHelper类似,仍然是CRUD,只不过1个是数据库中的,1个是文件的。
代码比较简单,每个函数的功能比较单一清晰,CRUD,主要是使用android.database.sqlite.SQLiteDatabase操作SQLite数据库。
package net.micode.fileexplorer.util; import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//存储favorite数据,到数据库
//SQLiteOpenHelper是一个帮助管理数据库和版本的工具类。
//通过继承并重载方法,快速实现了我们自己的Favorite表的CRUD。
//怎么感觉和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;
} //数据库创建,1个sql
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;
} //升级的时候,直接删除以前的数据库,如果存在的话
//版本号,没用上啊
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;
}
}
小米开源文件管理器MiCodeFileExplorer-源码研究(7)-Favorite收藏管理和SQLite数据库CRUD的更多相关文章
- 小米开源文件管理器MiCodeFileExplorer-源码研究(0)-初步研究
2011年对着书本Android应用开发揭秘,写了2个月的HelloWorld. 现在想复习并深入,我没有耐心再去一点点地敲代码了. 4年前自己是个学生,实习,现在有工作,只能业余时间研究. ...
- Android开源项目 Universal imageloader 源码研究之Lru算法
https://github.com/nostra13/Android-Universal-Image-Loader universal imageloader 源码研究之Lru算法 LRU - Le ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(1)-2个模型Model
上篇说到,把小米的Java代码整理成了5个包,其中1个是net.micode.fileexplorer.model.这个包就2个模型类,最基本了,FileInfo和FavoriteItem. pack ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(5)-AsyncTask异步任务
说明:本文的文字和代码,主要来自于网上的2篇文章. 第4篇的时候,提到了异步任务AsyncTask. 网上找了2篇文章学习下,copy网友的代码,稍微改了几个字,运行成功了. 在开发Android移动 ...
- 开源播放器ijkplayer源码结构
ijkplayer核心源码主要在ijkmedia文件夹下ijkplayer.ijksdl及ijkutils. 注:tag k0.3.1 player: remove ijkutil android相关 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析
AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(6)-媒体文件MediaFile和文件类型MimeUtils
接着之前的第4篇,本篇的2个类,仍然是工具类.MediaFile,媒体文件,定义了一大堆的常量,真正的有用的方法就几个.isAudioFileType.isVideoFileType之类的. Mime ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(2)-2个单实例工具类
从本篇开始,讲解net.micode.fileexplorer.util工具包中的类.这个包下的类,功能也比较单一和独立.很多代码的思想和实现,可以用于JavaWeb和Android等多种环境中. 一 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(3)-使用最多的工具类Util
Util.java,使用最广泛~代码中很多地方,都写了注释说明~基本不需要怎么解释了~ package net.micode.fileexplorer.util; import java.io.Fil ...
随机推荐
- [NOIP2012提高组]国王游戏
题目:洛谷P1080.Vijos P1779.codevs1198. 题目大意:国王和每个大臣左.右手各写了一个数.规定每个大臣得到的金币数为他前面所有人左手的数字的乘积除以他自己右手的数(向下取整) ...
- python虚拟环境virtualenv、virtualenv下运行IDLE、powershell 运行脚本由执行策略引起的问题
一.为什么要创建虚拟环境: 应为在开发中会有同时对一个包不同版本的需求,创建多个开发环境就能解决这个问题.或许也会有对python不同版本的需求,这就需要使用程序来管理不同的版本,virtualenv ...
- HDU 4960 Another OCD Patient 简单DP
思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: ...
- PatentTips - Supporting heterogeneous virtualization
BACKGROUND A virtual machine (VM) architecture logically partitions a physical machine, such that th ...
- error C2440: “static_cast”: 无法从“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)
转自原文 error C2440 “static_cast” 无法从“void (__thiscall C* )(void)... error C2440: “static_cast”: 无法从“LR ...
- 【JavaScript】分秒倒计时器
一.基本目标 在JavaScript设计一个分秒倒计时器,一旦时间完毕使button变成不可点击状态 详细效果例如以下图.为了说明问题.调成每50毫秒也就是每0.05跳一次表, 真正使用的时候,把wi ...
- KVM硬件辅助虚拟化之 EPT in Nested Virtualization
在嵌套虚拟环境(Nested Virtualization)下,执行在hypervisor上的Virtual Machine仍能够作为hypervisor去执行其他的Virutal Machine,而 ...
- 一些牛人的IOS博客,mark下慢慢学习
http://blog.devtang.com/ 唐巧的个人blog http://gracelancy.com/ Lancy's blog http://b ...
- JAVA类库LinkList的基本实现
写完调试了好久,边界不优点理,具体的请看JDK类库,下面仅仅是基本实现: import java.util.Iterator; /** * 类名:MyLinkedList 说明:LinkedList的 ...
- 如何组织CSS?
前端工程师在开发一个单页面或者小网站的时候有可能不会在意CSS的组织问题,但如果要开发一个中大型的网站,就要好好的组织CSS文件,不然会增加维护成本,整个网站的结构也没条理性. 如何组织CSS?一般常 ...