安卓: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
原创粉丝点击