2019独角兽企业重金招聘Python工程师标准>>>

CursorLoader的相关api,自行查询官网

实现效果: ContentPorvider情况下的CurSorLoader自动刷新 官方规定:CursorLoader的API必须使用ContentProvider才能实现数据加载和自动刷新

自定义ContentProvier:

public class MyContentProvider extends ContentProvider {

/*  * * 数据库发生改变时如何自动更新UI*  过程:数据库---contentprovider/contentresolver*             ---cursor的观察者-数据源发生改变*             ---cursorloader重新加载数据*             ---cursoradapter的changercursor提换数据*  做法:*  1. 对cursor数据设置监听的url,即在contentprovider中query()调用(或者loader的loadeingbackground())调用cursor.setNotificationUri()*  2.ContentProvider的insert()、update()、delete()等方法中调用ContentResolver的notifyChange()方法*/public static final int TABLE1_DIR = 0;
public static final int TABLE1_ITEM = 1;
public static final String AUTHORITY = "com.example.datebasedemo.provider";
private static UriMatcher uriMatcher;
private MySQLite dbHelper;
static {uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);uriMatcher.addURI(AUTHORITY, MySQLite.TABLENAME, TABLE1_DIR);uriMatcher.addURI(AUTHORITY, MySQLite.TABLENAME + "/#", TABLE1_ITEM);
}@Override
public boolean onCreate() {dbHelper = new MySQLite(getContext());return true;
}@Override
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {SQLiteDatabase db = dbHelper.getReadableDatabase();Cursor cursor = null;switch (uriMatcher.match(uri)) {case TABLE1_DIR:cursor = db.query(MySQLite.TABLENAME, projection, selection,selectionArgs, null, null, sortOrder);break;case TABLE1_ITEM:String bookId = uri.getPathSegments().get(1);cursor = db.query(MySQLite.TABLENAME, projection,MySQLite.COLUMN_ID + "= ?", new String[] { bookId }, null,null, sortOrder);break;default:break;}/** 在query()设置NotificationUri监听* 缺点:上千次并发调用会造成gc操作* */if (cursor != null) {cursor.setNotificationUri(getContext().getContentResolver(), uri);}return cursor;
}@Override
public Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = dbHelper.getWritableDatabase();Uri uriReturn = null;switch (uriMatcher.match(uri)) {case TABLE1_DIR:case TABLE1_ITEM:long newTableId = db.insert(MySQLite.TABLENAME, null, values);uriReturn = Uri.parse("content://" + AUTHORITY + "/"+ MySQLite.TABLENAME + "/" + newTableId);break;default:break;}getContext().getContentResolver().notifyChange(uri, null);return uriReturn;
}@Override
public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SQLiteDatabase db = dbHelper.getWritableDatabase();int updatedRows = 0;switch (uriMatcher.match(uri)) {case TABLE1_DIR:updatedRows = db.update(MySQLite.TABLENAME, values, selection,selectionArgs);break;case TABLE1_ITEM:String bookId = uri.getPathSegments().get(1);updatedRows = db.update(MySQLite.TABLENAME, values,MySQLite.COLUMN_ID + "= ?", new String[] { bookId });break;}if (updatedRows > 0) {getContext().getContentResolver().notifyChange(uri, null);}return updatedRows;
}@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = dbHelper.getWritableDatabase();int deletedRows = 0;switch (uriMatcher.match(uri)) {case TABLE1_DIR:deletedRows = db.delete(MySQLite.TABLENAME, selection,selectionArgs);break;case TABLE1_ITEM:String bookId = uri.getPathSegments().get(1);deletedRows = db.delete(MySQLite.TABLENAME, MySQLite.COLUMN_ID+ "= ?", new String[] { bookId });break;}getContext().getContentResolver().notifyChange(uri, null);return deletedRows;
}@Override
public String getType(Uri uri) {switch (uriMatcher.match(uri)) {case TABLE1_DIR:return "vnd.android.cursor.dir/vnd." + AUTHORITY+ MySQLite.TABLENAME;case TABLE1_ITEM:return "vnd.android.cursor.item/vnd." + AUTHORITY+ MySQLite.TABLENAME;default:break;}return null;
}

}

注册contentProvider:

 <provider android:name="com.example.datebasedemo.db.MyContentProvider"android:authorities="com.example.datebasedemo.provider"></provider>

自定义SQLiteOpenHelper:

public class MySQLite extends SQLiteOpenHelper {

public static final String DATABASENAME = "Book.db";
public static final String TABLENAME = "Book";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ID = "_id";
public static final String CRATETABLE_SQL = "create table " + MySQLite.TABLENAME+ "(" + MySQLite.COLUMN_ID + " integer primary key autoincrement, "+ MySQLite.COLUMN_NAME + " text" + ")";public MySQLite(Context context) {super(context, MySQLite.DATABASENAME, null, 1);
}@Override
public void onCreate(SQLiteDatabase db) {db.execSQL(CRATETABLE_SQL);Log.i("database", "create");
}@Override
public void onUpgrade(SQLiteDatabase db, int oldVerson, int newVerson) {db.execSQL("drop  table if exists "+TABLENAME);onCreate(db);
}

}

使用CursorLoader:

public class Zixuan_Fragment extends Fragment implements LoaderCallbacks<Cursor> {

private List<String> list = new ArrayList<String>();
private ListView listView;
private LinearLayout lin;
private ImageView img_add;
private SimpleCursorAdapter mCursorAdapter;
private View view;@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {getLoaderManager().initLoader(0, null, this);view = inflater.inflate(R.layout.zixuan_fragment, null);initViews(view);initListview(view);return view;
}private void initListview(View view) {listView = (ListView) view.findViewById(R.id.zixuan_listview);mCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item,null, new String[] { MySQLite.COLUMN_NAME },new int[] { R.id.item }, 0);listView.setAdapter(mCursorAdapter);}private void initViews(View v) {lin = (LinearLayout) v.findViewById(R.id.add_content);img_add = (ImageView) v.findViewById(R.id.addImg);img_add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(getActivity(), Manager.class));}});
}@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {return new android.support.v4.content.CursorLoader(getActivity(),ContentController.uri,new String[]{MySQLite.COLUMN_ID,MySQLite.COLUMN_NAME}, null, null, null);
}@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {mCursorAdapter.swapCursor(cursor);}@Override
public void onLoaderReset(Loader<Cursor> arg0) {mCursorAdapter.swapCursor(null);
}

}

转载于:https://my.oschina.net/u/2406195/blog/611036

CursorLoader的进级实践相关推荐

  1. 低通滤波器转带通滤波器公式由来_开关电源电磁兼容进级EMI传导输入滤波器的设计理论(EDTEST上海)...

    在刚刚结束的EDTEST-上海站:开关电源电磁兼容进级优化设计:对于有开关电源的产品及控制系统:其输入EMI低通滤波器放置在输入端对系统的EMS设计也是非常关键的! 再补充详解一下:我讲的开关电源系统 ...

  2. 感量越大抑制频率约低_开关电源电磁兼容进级-EMI传导输入滤波器的设计理论(ED-TEST上海)...

    在刚刚结束的EDTEST-上海站:开关电源电磁兼容进级优化设计:对于有开关电源的产品及控制系统:其输入EMI低通滤波器放置在输入端对系统的EMS设计也是非常关键的! 再补充详解一下:我讲的开关电源系统 ...

  3. 第十一课 Kubernetes生产级实践-ServiceMesh代表作istio

    第十一课 Kubernetes生产级实践-ServiceMesh代表作istio tags: k8s 慕课网 categories: ServiceMesh istio 文章目录 第十一课 Kuber ...

  4. Container峰会议题公开,顶级技术+生产级实践一网打尽

    2016年5月13日-15日,由CSDN重磅打造的2016中国云计算技术大会(CCTC 2016)将于5月13日-15日在北京举办,今年大会特设"中国Spark技术峰会".&quo ...

  5. Vue-vue-router.js路由--进级

    一:导航钩子 正如其名,vue-router 提供的导航钩子主要用来拦截导航,让它完成跳转或取消.有多种方式可以在路由导航发生时执行钩子:全局的, 单个路由独享的, 或者组件级的. 全局钩子 你可以使 ...

  6. 正则邮箱_正则表达式再进级

    断言 也称先行断言和后行断言为环视或预搜索! 先行断言和后行断言只有一个区别: 即先行断言往右看(限制后面字符),后行断言往左看(限制后面字符) 先行断言和后行断言总共有四种: 正向先行断言 反向先行 ...

  7. 轻量级CI/CD自动构建平台Gitea+Drone保姆级实践教程

    目录 1.关于Gitea 1.1 gitea特性 1.2 快速安装 1.2.1 环境依赖 1.2.2 安装gitea 1.2.3 启动 1.2.4 访问 1.2.5 创建一个测试仓库 2 关于Dron ...

  8. 腾讯云TDSQL数据库信创演进与实践

    日前,在中国电子信息行业联合会面向行业优秀品牌教育培育<2020-2021年度优秀创新软件产品>的征集中,腾讯云分布式数据库TDSQL管理系统成功入选,并荣获"年度优秀软件产品& ...

  9. Google Maps API 进级:通过XML文档加载Gpolyline或者Gpolygon

    转自:http://hi.baidu.com/xfm_zhr/blog/item/20e2e6f99c723e5e242df229.html 1.       通过XML文档加载Gpolyline或者 ...

最新文章

  1. VC下通过进程ID获取进程镜像文件路径的方法及其存在的缺陷
  2. 新手用python2还是3-Python 使用情况调查:2还是3?(附致歉声明)
  3. 微软Office Online服务安装部署及wopi代码实现--------Office Online服务器的安装
  4. 用计算机计算的手抄报内容,关于数学计算手抄报
  5. 《构架之美》阅读笔记六
  6. CSS清浮动处理(Clear与BFC)
  7. java listen_Java进阶-IO基础
  8. ASP.NET Core真实管道详解[2]:Server是如何完成针对请求的监听、接收与响应的【上】
  9. 工程量计算稿1.54安装教程 v1.54pjb
  10. android的sd卡分区,超强Android系统SD卡分区教程!
  11. j2se学习笔记-Enum枚举类型
  12. 自定义类型:枚举,结构体,联合体
  13. 一个“后浪”的狂欢,一群中年人的孤单!
  14. 常见浏览器及其内核(国际)
  15. 10倍杠杆炒股是什么意思
  16. 用jQuery访问指定元素的父元素
  17. 配置Kafka发送大消息
  18. python使用opencv对图像添加(高斯/椒盐/泊松/斑点)噪声
  19. Unity shader 使用 半兰伯特 (Half-Lambert)漫反射会使得模型在没有直射光的情况下漆黑一片
  20. 软件工程项目 四则运算表达式生成----5

热门文章

  1. 解决织梦(DedeCMS)系统自定义字段图片调用问题
  2. 容器弹性云底层原理揭秘
  3. Java高并发编程详解系列-ThreadGroup介绍
  4. 零值比较--BOOL,int,float,指针变量与零值比
  5. Golang程序性能分析(三)用pprof分析gRPC服务的性能
  6. 《Go 语言程序设计》读书笔记(十)反射
  7. 鼠标linux驱动安装失败,win7插入鼠标提示未能成功安装设备驱动程序怎么办
  8. php oop基础,php面向对象编程(oop)基础
  9. MongoDB中的索引操作
  10. springboot项目发布JAR包