FavoriteDatabaseHelper,存储favorite数据,到SQLite数据库。
SQLiteOpenHelper是一个帮助管理数据库和版本的工具类。
通过继承并重载方法,快速实现了我们自己的Favorite表的CRUD。
怎么感觉和FileOperationHelper类似,仍然是CRUD,只不过1个是数据库中的,1个是文件的。
代码比较简单,每个函数的功能比较单一清晰,CRUD,主要是使用android.database.sqlite.SQLiteDatabase操作SQLite数据库。

  1. package net.micode.fileexplorer.util;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. //存储favorite数据,到数据库
  9. //SQLiteOpenHelper是一个帮助管理数据库和版本的工具类。
  10. //通过继承并重载方法,快速实现了我们自己的Favorite表的CRUD。
  11. //怎么感觉和FileOperationHelper类似,仍然是CRUD,只不过1个是数据库中的,1个是文件的。
  12. public class FavoriteDatabaseHelper extends SQLiteOpenHelper {
  13.  
  14. //下面6个字段是数据库的名字和版本号、表的名字和3个字段
  15. private final static String DATABASE_NAME = "file_explorer";
  16.  
  17. private final static int DATABASE_VERSION = 1;
  18.  
  19. private final static String TABLE_NAME = "favorite";
  20.  
  21. public final static String FIELD_ID = "_id";
  22.  
  23. public final static String FIELD_TITLE = "title";
  24.  
  25. public final static String FIELD_LOCATION = "location";
  26.  
  27. private boolean firstCreate;
  28.  
  29. //数据库变化的时候,会通知其它监听器
  30. private FavoriteDatabaseListener mListener;
  31.  
  32. private static FavoriteDatabaseHelper instance;
  33.  
  34. public interface FavoriteDatabaseListener {
  35. void onFavoriteDatabaseChanged();
  36. }
  37.  
  38. //这个构造方法和下面的静态获得实例的方法,不太和谐啊~
  39. //乍一看,以为是单例模式呢,实则不是~
  40. public FavoriteDatabaseHelper(Context context, FavoriteDatabaseListener listener) {
  41. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  42. instance = this;
  43. mListener = listener;
  44. }
  45.  
  46. //这个地方感觉只是方便存储了一个类的实例,但不能保证这个类只有1个实例
  47. public static FavoriteDatabaseHelper getInstance() {
  48. return instance;
  49. }
  50.  
  51. //数据库创建,1个sql
  52. public void onCreate(SQLiteDatabase db) {
  53. String sql = "Create table " + TABLE_NAME + "(" + FIELD_ID + " integer primary key autoincrement,"
  54. + FIELD_TITLE + " text, " + FIELD_LOCATION + " text );";
  55. db.execSQL(sql);
  56. firstCreate = true;
  57. }
  58.  
  59. //升级的时候,直接删除以前的数据库,如果存在的话
  60. //版本号,没用上啊
  61. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  62. String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
  63. db.execSQL(sql);
  64. onCreate(db);
  65. }
  66.  
  67. //是否为第1次创建
  68. public boolean isFirstCreate() {
  69. return firstCreate;
  70. }
  71.  
  72. //判断1个文件路径是否已经存在,或者说是否是Favorite文件
  73. public boolean isFavorite(String path) {
  74. String selection = FIELD_LOCATION + "=?";
  75. String[] selectionArgs = new String[] {
  76. path
  77. };
  78. SQLiteDatabase db = this.getReadableDatabase();
  79. Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, null, null, null);
  80. if (cursor == null)
  81. return false;
  82. boolean ret = cursor.getCount() > 0;
  83. cursor.close();
  84. return ret;
  85. }
  86.  
  87. //获得Favorite表的游标
  88. public Cursor query() {
  89. SQLiteDatabase db = this.getReadableDatabase();
  90. Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
  91. return cursor;
  92. }
  93.  
  94. //插入一条记录
  95. public long insert(String title, String location) {
  96. if (isFavorite(location))
  97. return -1;
  98.  
  99. SQLiteDatabase db = this.getWritableDatabase();
  100. long ret = db.insert(TABLE_NAME, null, createValues(title, location));
  101. mListener.onFavoriteDatabaseChanged();
  102. return ret;
  103. }
  104.  
  105. //根据id,删除一条记录。如果需要,然后通知相关监听器
  106. public void delete(long id, boolean notify) {
  107. SQLiteDatabase db = this.getWritableDatabase();
  108. String where = FIELD_ID + "=?";
  109. String[] whereValue = {
  110. Long.toString(id)
  111. };
  112. db.delete(TABLE_NAME, where, whereValue);
  113.  
  114. if (notify)
  115. mListener.onFavoriteDatabaseChanged();
  116. }
  117.  
  118. //根据位置删除1条记录,一定通知相关监听器
  119. public void delete(String location) {
  120. SQLiteDatabase db = this.getWritableDatabase();
  121. String where = FIELD_LOCATION + "=?";
  122. String[] whereValue = {
  123. location
  124. };
  125. db.delete(TABLE_NAME, where, whereValue);
  126. mListener.onFavoriteDatabaseChanged();
  127. }
  128.  
  129. //更新1条记录
  130. public void update(int id, String title, String location) {
  131. SQLiteDatabase db = this.getWritableDatabase();
  132. String where = FIELD_ID + "=?";
  133. String[] whereValue = {
  134. Integer.toString(id)
  135. };
  136. db.update(TABLE_NAME, createValues(title, location), where, whereValue);
  137. mListener.onFavoriteDatabaseChanged();
  138. }
  139.  
  140. private ContentValues createValues(String title, String location) {
  141. ContentValues cv = new ContentValues();
  142. cv.put(FIELD_TITLE, title);
  143. cv.put(FIELD_LOCATION, location);
  144. return cv;
  145. }
  146. }

小米开源文件管理器MiCodeFileExplorer-源码研究(7)-Favorite收藏管理和SQLite数据库CRUD的更多相关文章

  1. 小米开源文件管理器MiCodeFileExplorer-源码研究(0)-初步研究

    2011年对着书本Android应用开发揭秘,写了2个月的HelloWorld.   现在想复习并深入,我没有耐心再去一点点地敲代码了.   4年前自己是个学生,实习,现在有工作,只能业余时间研究. ...

  2. Android开源项目 Universal imageloader 源码研究之Lru算法

    https://github.com/nostra13/Android-Universal-Image-Loader universal imageloader 源码研究之Lru算法 LRU - Le ...

  3. 小米开源文件管理器MiCodeFileExplorer-源码研究(1)-2个模型Model

    上篇说到,把小米的Java代码整理成了5个包,其中1个是net.micode.fileexplorer.model.这个包就2个模型类,最基本了,FileInfo和FavoriteItem. pack ...

  4. 小米开源文件管理器MiCodeFileExplorer-源码研究(5)-AsyncTask异步任务

    说明:本文的文字和代码,主要来自于网上的2篇文章. 第4篇的时候,提到了异步任务AsyncTask. 网上找了2篇文章学习下,copy网友的代码,稍微改了几个字,运行成功了. 在开发Android移动 ...

  5. 开源播放器ijkplayer源码结构

    ijkplayer核心源码主要在ijkmedia文件夹下ijkplayer.ijksdl及ijkutils. 注:tag k0.3.1 player: remove ijkutil android相关 ...

  6. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析

    AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...

  7. 小米开源文件管理器MiCodeFileExplorer-源码研究(6)-媒体文件MediaFile和文件类型MimeUtils

    接着之前的第4篇,本篇的2个类,仍然是工具类.MediaFile,媒体文件,定义了一大堆的常量,真正的有用的方法就几个.isAudioFileType.isVideoFileType之类的. Mime ...

  8. 小米开源文件管理器MiCodeFileExplorer-源码研究(2)-2个单实例工具类

    从本篇开始,讲解net.micode.fileexplorer.util工具包中的类.这个包下的类,功能也比较单一和独立.很多代码的思想和实现,可以用于JavaWeb和Android等多种环境中. 一 ...

  9. 小米开源文件管理器MiCodeFileExplorer-源码研究(3)-使用最多的工具类Util

    Util.java,使用最广泛~代码中很多地方,都写了注释说明~基本不需要怎么解释了~ package net.micode.fileexplorer.util; import java.io.Fil ...

随机推荐

  1. shell单引号屏蔽变量方法

    [goforit ~]$ name="玖零後大叔" [goforit~]$ echo $name 玖零後大叔 [goforit ~]$ echo "$name" ...

  2. ECNUOJ 2856 仰望星空

    仰望星空 Time Limit:1000MS Memory Limit:65536KBTotal Submit:373 Accepted:145 Description  我仰望星空, 它是那样辽阔而 ...

  3. Android基础新手教程——4.3.2 BroadcastReceiver庖丁解牛

    Android基础新手教程--4.3.2 BroadcastReceiver庖丁解牛 标签(空格分隔): Android基础新手教程 本节引言: 上节我们对BroadcastReceiver已经有了一 ...

  4. Qt编译OpenGL程序遇到的问题

    软件版本号: Qt 4.8.5 依照网上的例程(http://www.qiliang.net/old/nehe_qt/lesson01.html),跑了一下基于Qt Creator的OpenGL.因为 ...

  5. Activity 中的Toast在Activity销毁后报错,解决方法,把context改成应用的

    ToastUtil.showShort(context, R.string.connection_fail); 改成 ToastUtil.showShort(BusinesslinkApplicati ...

  6. 绿色便携版Lazarus的制作教程

    本文来源: www.fpccn.com 原作者:逍遥派掌门人 http://msdn.microsoft.com/zh-cn/library/windows/apps/hh452791.aspx 本教 ...

  7. VC双缓冲画图技术介绍

    双缓冲画图,它是一种主要的图形图像画图技术.首先,它在内存中创建一个与屏幕画图区域一致的对象,然后将图形绘制到内存中的这个对象上,最后把这个对象上的图形数据一次性地拷贝并显示到屏幕上. 这样的技术能够 ...

  8. 记一次struts2漏洞修复带来的问题

    struts2作为万年漏洞王,感觉已经被弃如敝屣了,除了一些古老的项目,比如我手上的一个项目,以前每次出现漏洞就如临大敌,手忙脚乱的赶在公司红头文件发出来前修复它.然后改了一两次后毅然决然用别的框架代 ...

  9. hdoj--2955--Robberies(背包好题)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. 79.express里面的app.configure作用

    以下摘自 express 3.0 的 文档 app.configure([env], callback) Conditionally invoke callback when env matches ...