安卓:ContentProvider之服务端提供接口,客户端通过接口对数据库中的数据进行操作

客户端操作结果图:


服务端项目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>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值