小米开源文件管理器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 ...
随机推荐
- shell单引号屏蔽变量方法
[goforit ~]$ name="玖零後大叔" [goforit~]$ echo $name 玖零後大叔 [goforit ~]$ echo "$name" ...
- ECNUOJ 2856 仰望星空
仰望星空 Time Limit:1000MS Memory Limit:65536KBTotal Submit:373 Accepted:145 Description 我仰望星空, 它是那样辽阔而 ...
- Android基础新手教程——4.3.2 BroadcastReceiver庖丁解牛
Android基础新手教程--4.3.2 BroadcastReceiver庖丁解牛 标签(空格分隔): Android基础新手教程 本节引言: 上节我们对BroadcastReceiver已经有了一 ...
- Qt编译OpenGL程序遇到的问题
软件版本号: Qt 4.8.5 依照网上的例程(http://www.qiliang.net/old/nehe_qt/lesson01.html),跑了一下基于Qt Creator的OpenGL.因为 ...
- Activity 中的Toast在Activity销毁后报错,解决方法,把context改成应用的
ToastUtil.showShort(context, R.string.connection_fail); 改成 ToastUtil.showShort(BusinesslinkApplicati ...
- 绿色便携版Lazarus的制作教程
本文来源: www.fpccn.com 原作者:逍遥派掌门人 http://msdn.microsoft.com/zh-cn/library/windows/apps/hh452791.aspx 本教 ...
- VC双缓冲画图技术介绍
双缓冲画图,它是一种主要的图形图像画图技术.首先,它在内存中创建一个与屏幕画图区域一致的对象,然后将图形绘制到内存中的这个对象上,最后把这个对象上的图形数据一次性地拷贝并显示到屏幕上. 这样的技术能够 ...
- 记一次struts2漏洞修复带来的问题
struts2作为万年漏洞王,感觉已经被弃如敝屣了,除了一些古老的项目,比如我手上的一个项目,以前每次出现漏洞就如临大敌,手忙脚乱的赶在公司红头文件发出来前修复它.然后改了一两次后毅然决然用别的框架代 ...
- hdoj--2955--Robberies(背包好题)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 79.express里面的app.configure作用
以下摘自 express 3.0 的 文档 app.configure([env], callback) Conditionally invoke callback when env matches ...