安卓:ContentProvider之服务端提供接口,客户端通过接口对数据库中的数据进行操作
2015-09-21 17:42
756 查看
客户端操作结果图:
服务端项目app:
主逻辑代码和布局文件依创建好项目时的不需要动
继承ContentProvider的自写的类的逻辑代码:
清单文件中需加入:
<!-- android:name="包名+类名"
android:authorities="com.qianfeng.mycontenprovider"一般是包名+类名 (小写) 让权限唯一
android:exported="true"其他程序可以使用
-->
<provider
android:name="com.example.day16_contentprovider.MyContentProvide"
android:authorities="com.mycontentprovide"
android:exported="true" >
</provider>
创建数据库的类:
客户端项目app:
主逻辑代码文件:
主布局文件:
适配器布局文件:
服务端项目app:
主逻辑代码和布局文件依创建好项目时的不需要动
继承ContentProvider的自写的类的逻辑代码:
清单文件中需加入:
<!-- android:name="包名+类名"
android:authorities="com.qianfeng.mycontenprovider"一般是包名+类名 (小写) 让权限唯一
android:exported="true"其他程序可以使用
-->
<provider
android:name="com.example.day16_contentprovider.MyContentProvide"
android:authorities="com.mycontentprovide"
android:exported="true" >
</provider>
<span style="font-size:18px;">package com.example.day16_contentprovider; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; public class MyContentProvide extends ContentProvider{ MyOpenHelper helper; //创建UriMatcher对象进行匹配 static UriMatcher urimatcher=new UriMatcher(UriMatcher.NO_MATCH); static int QUERY=1;//进行查询时匹配的标志 static int INSERT=2;//进行添加时匹配的标志 static int UPDATE=3;//进行修改时匹配的标志 static int DELETE=4;//进行删除时匹配的标志 static { /** * 参数1:权限 在清单文件注册时写入 * 参数2:访问当前Uri的路径 * 参数3:客户端和当前程序的uri是否匹配返回的值 code */ urimatcher.addURI("com.mycontentprovide", "query", QUERY); urimatcher.addURI("com.mycontentprovide", "insert", INSERT); urimatcher.addURI("com.mycontentprovide", "update", UPDATE); urimatcher.addURI("com.mycontentprovide", "delete", DELETE); // content://authority/path // content://com.mycontentprovide/query // content://com.mycontentprovide/insert // content://com.mycontentprovide/update // content://com.mycontentprovide/delete }; //类加载时调用,提供给本程序用 @Override public boolean onCreate() { helper=new MyOpenHelper(getContext()); return false; } //提供给客户端程序使用,即访问当前应用程序返回CurSor对象 @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db=helper.getReadableDatabase(); Cursor c=null; if(urimatcher.match(uri)==QUERY) { c=db.query("happy", projection, selection, selectionArgs, null, null, sortOrder); } return c; } //提供给客户端程序使用,向本应用程序插入数据返回插入数据的uri @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db=helper.getReadableDatabase(); if(urimatcher.match(uri)==INSERT) { db.insert("happy", null, values); } return null; } //提供给客户端程序使用,修改本应用程序的数据,返回int类型数据表示修改了几条数据 @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db=helper.getReadableDatabase(); int count=0; if(urimatcher.match(uri)==UPDATE) { count=db.update("happy", values, selection, selectionArgs); } return count; } //提供给客户端程序使用,删除本应用程序的数据返回int类型数据代表删除了几条 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db=helper.getReadableDatabase(); int count=0; if(urimatcher.match(uri)==DELETE) { count=db.delete("happy", selection, selectionArgs); } return count; } //返回Uri的类型 @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } } </span>
创建数据库的类:
<span style="font-size:18px;">package com.example.day16_contentprovider; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper{ static String NAME="total.db"; static int VERSION=1; public MyOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public MyOpenHelper(Context context) { super(context, NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql="create table happy(_id Integer primary key,name varchar(20),age Integer)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } </span>
客户端项目app:
主逻辑代码文件:
<span style="font-size:18px;">package com.example.day16_contentproviderclient1; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.widget.SimpleCursorAdapter; import android.view.View; import android.widget.Button; import android.widget.ListView; public class MainActivity extends Activity { // content://authority/path // content://com.mycontentprovide/query // content://com.mycontentprovide/insert // content://com.mycontentprovide/update // content://com.mycontentprovide/delete Button add,update,delete; ListView lv; ContentResolver cr; static String query_path="content://com.mycontentprovide/query"; static String insert_path="content://com.mycontentprovide/insert"; String update_path="content://com.mycontentprovide/update"; String delete_path="content://com.mycontentprovide/delete"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); add=(Button) findViewById(R.id.add); update=(Button) findViewById(R.id.update); delete=(Button) findViewById(R.id.delete); lv=(ListView) findViewById(R.id.lv); cr=getContentResolver(); Cursor c=cr.query(Uri.parse(query_path), null, null, null, null); SimpleCursorAdapter adapter=new SimpleCursorAdapter(getApplicationContext(), R.layout.item, c, new String[]{"name","age"}, new int[]{R.id.name,R.id.age}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lv.setAdapter(adapter); } public void click(View v) { switch(v.getId()) { case R.id.add: insertData(); break; case R.id.update: break; case R.id.delete: break; } } public void insertData() { ContentValues values=new ContentValues(); values.put("name", "李灰灰"); values.put("age", 23); cr.insert(Uri.parse(insert_path), values); values=new ContentValues(); values.put("name", "小灰狼"); values.put("age", 18); cr.insert(Uri.parse(insert_path), values); values=new ContentValues(); values.put("name", "小红帽"); values.put("age", 21); cr.insert(Uri.parse(insert_path), values); } public void updateData() { } } </span>
主布局文件:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" android:onClick="click"/> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" android:onClick="click"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:onClick="click"/> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </span>
适配器布局文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </span>
相关文章推荐
- Mysql的安装
- Nodejs 使用Oracle数据库
- mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
- 在Mysql 众多表中查找一个表名或者字段名
- MongoDB CRUD操作
- zabbix监控mysql状态
- mysql 查询条件中文问题
- 【SQL】多个表的查询
- 安装lamp之 一键安装单实例mysql
- mysql设置服务器编码
- sql与oracle中有关case和decode的用法(行转列)及比较(转
- 如何在mysql中优雅的解决精确到毫秒的问题?
- 为什么在WinXP系统上无法安装SQL2000的服务器组件?
- SQL 数据库中如何自动生成订单号
- 安卓:查询sqlite数据库中的数据,分页加载显示出来
- QUARTZ任务不写入数据库
- Oracle Jdbc 防sql注入
- 备忘:连接数据库的写法
- Mysql免安装版安装配置及常用操作
- SQL Server数据库优化的10多种方法