安卓:ContentProvider之服务端提供接口,客户端通过接口对数据库中的数据进行操作
来源:互联网 发布:易建联卧推数据 编辑:程序博客网 时间:2024/05/16 15:17
客户端操作结果图:
服务端项目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};//类加载时调用,提供给本程序用@Overridepublic boolean onCreate() {helper=new MyOpenHelper(getContext());return false;}//提供给客户端程序使用,即访问当前应用程序返回CurSor对象@Overridepublic 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@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db=helper.getReadableDatabase();if(urimatcher.match(uri)==INSERT){db.insert("happy", null, values);}return null;}//提供给客户端程序使用,修改本应用程序的数据,返回int类型数据表示修改了几条数据@Overridepublic 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类型数据代表删除了几条@Overridepublic 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的类型@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn 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);}@Overridepublic void onCreate(SQLiteDatabase db) {String sql="create table happy(_id Integer primary key,name varchar(20),age Integer)";db.execSQL(sql);}@Overridepublic 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/deleteButton 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>
0 0
- 安卓:ContentProvider之服务端提供接口,客户端通过接口对数据库中的数据进行操作
- webservice实际应用之调用外部系统提供的服务端地址,写java的webservice客户端程序并通过接口给外部系统传数据
- 通过ICE接口对hbase进行文件操作
- 安卓通过JDBC进行数据库操作
- Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据
- 【转载】Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据,作者:Jaiky_杰哥
- Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据
- Android 对Android客户端与服务端传递的Json数据进行加密、解密操作
- 接口加密《一》:移动应用中,通过在客户端对访问的url进行加密处理来保护服务器上的数据
- 苹果客户端支付后,服务器端对数据进行二次验证接口开发
- 使用系统API对安卓数据库进行操作
- 写接口时,对数据进行验证
- 写接口时,对数据进行验证
- ContentProvider提供的对数据库批量操作的方法和对数据库变化监控的方法
- 安卓客户端,服务端
- 怎么对ContentProvider中的数据变化进行监控
- 对mySQL数据库进行数据操作之添加
- 利用mysql提供的c语言接口操作数据库
- Android——启动过程详解
- Android 自定义TitleBar替换ActionBar的另类方式
- 【Android Studio】制作清晰Gif截图以作为项目的运行效果
- Java中String类对象的初始化
- Android核心基础-12.多线程断点续传下载
- 安卓:ContentProvider之服务端提供接口,客户端通过接口对数据库中的数据进行操作
- mapreduce中的sequenceFile类,MapFile解析
- Ubuntu 12.04(64位)安装Android Studio
- 内存溢出解决办法
- Linux战地日记——find命令
- 【软考】软考视频总结
- 【15.09.21】一些容易被忽视的linux命令(一)
- Linux下的共享内存(01)---查看和释放共享内存
- 怎样才是理想的程序员