给大家简述下ContentProvider(内容提供者)与ContentResolver(内容访问者)如何使用。
来源:互联网 发布:点击链接直接下载 js 编辑:程序博客网 时间:2024/06/06 01:58
就给大家简单的讲解下本人对 ContentProvider内容提供者 与ContentResolver内容访问者的基础了解吧。
首先在SQLite3工具中,有可以添加或者查询的基础上,也就是把这个项目当做ContentProvider(内容提供者来使用)
也就是为了给ContentResolver(内容访问者)访问做相应的铺垫吧
步骤1:现在SQLite项目中新建一个包,包名随便然后建一个Class文件也就是下面这个类 同时继承ContentProvider重写其中的六个方法。
public class MyContentProvider extends ContentProvider { private SQLiteDatabase db; private UriMatcher uriMatcher; @Override public boolean onCreate() { Log.i("test","onCreate");
//创建数据库 DbUtil dbUtil = new DbUtil(getContext(), "G150820.db", null, 2); db = dbUtil.getReadableDatabase(); //实例化URI匹配器 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); //添加规则 //0.1 查询所有 uriMatcher.addURI("com.example.g150820_sqlite666.PERSON","student",1); //0.2 查询单个 uriMatcher.addURI("com.example.g150820_sqlite666.PERSON","student/#",2); return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { //根据URi匹配器开始匹配uri int code=uriMatcher.match(uri); switch (code){ case 1: //查询所有 Log.i("test","query所有"); //获取数据库中的所有数据 return db.query(false,"student",projection,selection,selectionArgs,null,null,sortOrder,null); case 2: //查询单个 //获取 #的值 long id= ContentUris.parseId(uri); Log.i("test","query单个"); return db.rawQuery("select * from student where _id=?",new String[]{id+""}); } return null; } @Nullable @Override public String getType(Uri uri) { Log.i("test","getType"); return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { Log.i("test","insert"); return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.i("test","delete"); return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.i("test","update"); return 0; }}注意:记得写完新的类文件的时候记得配置清单文件(也就是AndroidManlfest.xml),如下。
<provider android:authorities="com.example.g150820_sqlite666.PERSON" android:name="provider.MyContentProvider" android:exported="true" ></provider>
authorities中配置的也就是SQLite中的包名,PERSON随意加
步骤2:新建一个ContentResolver内容者
直接上代码
public class MainActivity extends ListActivity{ private ContentResolver cr; private EditText et_main_id; private Uri uri; private ListView listView; private SQLiteDatabase sqLiteDatabase; private Cursor cursor; private SimpleCursorAdapter simpleCursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取内容访问者 cr = getContentResolver(); et_main_id = (EditText) findViewById(R.id.et_main_id); //显示ListView数据 listView = getListView(); //创建数据库 DbUtil dbUtil = new DbUtil(this, "G150820.db", null, 2); sqLiteDatabase = dbUtil.getReadableDatabase(); //查询所有的数据// queryAll(); //Cursor 游标 //循环游标 //实例化适配器BaseAdapter //把游标中的数据 全部给了List //设置适配器 simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, new String[]{"_id", "name", "age"}, new int[]{R.id.tv_item_list_id, R.id.tv_item_list_name, R.id.tv_item_list_age}); listView.setAdapter(simpleCursorAdapter); //给ListView设置长按事件 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { LinearLayout root = (LinearLayout) view; String id = ((TextView) root.findViewById(R.id.tv_item_list_id)).getText().toString(); String name = ((TextView) root.findViewById(R.id.tv_item_list_name)).getText().toString(); String age = ((TextView) root.findViewById(R.id.tv_item_list_age)).getText().toString(); final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); final AlertDialog alertDialog = builder.create(); //点击空白处,不取消对话框 alertDialog.setCancelable(false); View v = View.inflate(MainActivity.this, R.layout.dialog_list, null); TextView tv_dialog_list_id = (TextView) v.findViewById(R.id.tv_dialog_list_id); EditText et_dialog_list_name = (EditText) v.findViewById(R.id.et_dialog_list_name); EditText et_dialog_list_age = (EditText) v.findViewById(R.id.et_dialog_list_age); ((Button) v.findViewById(R.id.btn_dialog_list_ok)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "执行修改语句", Toast.LENGTH_SHORT).show();// 关闭当前对话框 alertDialog.dismiss(); } }); ((Button) v.findViewById(R.id.btn_dialog_list_nook)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {// 关闭当前对话框 alertDialog.dismiss(); } }); tv_dialog_list_id.setText("修改:" + id); et_dialog_list_name.setText(name); et_dialog_list_age.setText(age); alertDialog.setView(v); alertDialog.show(); return true; } }); } public void getData(View view) { //判断输入框的值是否为空 if (TextUtils.isEmpty(et_main_id.getText().toString())) { //查询所有
//这里content是使用Uri匹配器必要的协议 uri = Uri.parse("content://com.example.g150820_sqlite666.PERSON/student"); } else { //查询单个 //0.1直接query传参 //02.类似web http://localhost:8080/webProject/xxx.action?id=3 //0.3 Uri匹配器 String id = et_main_id.getText().toString(); uri = Uri.parse("content://com.example.g150820_sqlite666.PERSON/student/" + id); } Cursor cursor = cr.query(uri, null, null, null, null); //SimleCursorAdapter(展示在ListView中的适配器) while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.i("test", id + " " + name + " " + age); //通知适配器发送变化 simpleCursorAdapter.changeCursor(cursor); } } public void queryAll(String... str) { String name = null; int length = str.length; if (length == 0) {// Toast.makeText(MainActivity.this, "查询所有", Toast.LENGTH_SHORT).show(); //?,? //下标 pageSize //pageNum //下标=(pageNum-1)*pageSize cursor = sqLiteDatabase.rawQuery("select * from student limit ?,?", new String[]{2 + "", 2 + ""}); } else { name = str[0]; Toast.makeText(MainActivity.this, "模糊查询:" + name, Toast.LENGTH_SHORT).show(); cursor = sqLiteDatabase.rawQuery("select * from student where name like ?", new String[]{"%" + name + "%"}); } }我这里用LIstActivity的原因就是调用系统自带的id,这里用到了两种查询也就是查询全部与查询单个
同时查询单个有3中方式
1:直接query传参
2.类似web http://localhost:8080/webProject/xxx.action?id=3
3.Uri匹配器
所以这要看个人爱好了,但是我这里查询单个用的是Uri匹配器,判断是否是查询所有还是单个挺简单的
在一个点击监听事件中写一个判断,如果查询为空则查询的是所有
uri = Uri.parse("content://com.example.g150820_sqlite666.PERSON/student");如果是else则是相反的查询的是单个
uri = Uri.parse("content://com.example.g150820_sqlite666.PERSON/student/" + id);
Uri匹配器中实例化步骤一已经说到了
//实例化URI匹配器
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
添加规则上面也说到了//添加规则
//0.1 查询所有
uriMatcher.addURI("com.example.g150820_sqlite666.PERSON","student",1);//0.2 查询单个
uriMatcher.addURI("com.example.g150820_sqlite666.PERSON","student/#",2);
总结:
总之不管是查询单个还是查询全部 都是调用了query中的方法 ,这样一来我们也可以实现MyContentProvider 中我们常用的增删改查了是吧,我这里是写的从SQLite中获取过来的数据展示在我ContentResolver内容访问者中并做了相应的长按删除,如果不需要的话可以跳过,大概就说到这里了吧 ,本人也是用自己的理解方式简述了 ContentProvider内容提供者 与ContentResolver内容访问者的粗略了解,有不当的或者不理解的地方还请多多见谅。
2 0
- 给大家简述下ContentProvider(内容提供者)与ContentResolver(内容访问者)如何使用。
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider(内容提供者)与ContentResolver(内容访问者)
- ContentProvider内容提供者与ContentResolver内容访问者(二)
- 25.ContentProvider自定义内容提供者与ContentResolver内容访问者
- Android之ContentProvider(内容的提供者)与ContentResolver(内容访问者)
- ContentProvider内容提供者 和 ContentResolver内容访问者
- Android----ContentProvider(内容提供者) ContentResolver(内容访问者)
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- 使用内容提供者ContentProvider与内容访问者ContentResolver实现拿到另外一个项目的数据库数据
- 内容提供者ContentProvider与内容接受者ContentResolver的使用详解
- Android 中ContentProvider内容提供者和ContentResolver内容访问者
- 内容提供者 ContentProvider 内容访问者 ContentResolver 框架搭建
- Android:内容提供者(ContentProvider)和内容访问者(ContentResolver)
- 谈谈我对运维的理解
- Handler源码分析
- RecyclerView 实现瀑布流及点击事件,含点击回调的接口
- java和javascript 分别获取当前时间
- PAT BASIC 1014 福尔摩斯的约会
- 给大家简述下ContentProvider(内容提供者)与ContentResolver(内容访问者)如何使用。
- "请停用以开发者模式运行的扩展程序" 的解决办法
- 1031. Hello World for U 解析
- 冒泡排序
- Android中微信抢红包助手的实现
- mini-batch 梯度下降
- 采药
- hbase--Hbase 建表基本命令总结
- Leet Code OJ 17. Letter Combinations of a Phone Number