android 玩转ContentProvider之二--实现多个ContentProvider对多张表进行操作

本人原创作品,谢绝转载!

    上一篇android 玩转ContentProvider之一--实现ContentProvider操作数据库讲了ContentProvider基本用法,下面提高一下,实现两个ContentProvider分别对表进行操作。

    其它地方跟一个ContentProvider操作一张表都是一样的,唯一区别是authority,在声明的时候要注意,因为要声明两个ContentProvider,所以authority也要不一样,否则就会因为找不到对应的ContentProvider处理而出现下面的异常:

03-17 15:29:17.494: E/AndroidRuntime(529): Caused by: java.lang.IllegalArgumentException: Unknown URI content://com.jacp.provider.demo.company/leaders
03-17 15:29:17.494: E/AndroidRuntime(529): at com.jacp.demo.provider.ProgrammerProvider.insert(ProgrammerProvider.java:88)
03-17 15:29:17.494: E/AndroidRuntime(529): at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
03-17 15:29:17.494: E/AndroidRuntime(529): at android.content.ContentResolver.insert(ContentResolver.java:587)
03-17 15:29:17.494: E/AndroidRuntime(529): at com.jacp.demo.ContentProviderDemoActivity.insertLeader(ContentProviderDemoActivity.java:78)
03-17 15:29:17.494: E/AndroidRuntime(529): at com.jacp.demo.ContentProviderDemoActivity.testLeader(ContentProviderDemoActivity.java:69)
03-17 15:29:17.494: E/AndroidRuntime(529): at com.jacp.demo.ContentProviderDemoActivity.onCreate(ContentProviderDemoActivity.java:24)
03-17 15:29:17.494: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-17 15:29:17.494: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

    下面贴代码,有些要注意的地方也在代码中做了注释。

SQLite数据库直接操作类:

DatabaseHelper.java

package com.jacp.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.jacp.demo.provider.Provider;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "jacp_demo.db";
	private static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	// 创建programmer表
        db.execSQL("CREATE TABLE " + Provider.ProgrammerColumns.TABLE_NAME + " ("
                + Provider.ProgrammerColumns._ID + " INTEGER PRIMARY KEY,"
                + Provider.ProgrammerColumns.NAME + " TEXT,"
                + Provider.ProgrammerColumns.AGE + " INTEGER"
                + ");");
        
        // 创建leader表
        db.execSQL("CREATE TABLE " + Provider.LeaderColumns.TABLE_NAME + " ("
        		+ Provider.LeaderColumns._ID + " INTEGER PRIMARY KEY,"
        		+ Provider.LeaderColumns.NAME + " TEXT,"
        		+ Provider.LeaderColumns.TITLE + " TEXT,"
        		+ Provider.LeaderColumns.LEVEL + " INTEGER"
        		+ ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Provider.ProgrammerColumns.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Provider.LeaderColumns.TABLE_NAME);
        onCreate(db);
    }
}

保存跟数据库及表有关的常量:
Provider.java

package com.jacp.demo.provider;

import android.net.Uri;
import android.provider.BaseColumns;

/**
 * 保存数据库中的常量
 * @author jacp
 *
 */
public class Provider {
	
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.jacp.demo";

    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.jacp.demo";

    /**
     * 保存programmer表中用到的常量
     * @author jacp
     *
     */
	public static final class ProgrammerColumns implements BaseColumns {
		// 注意两张表的authority要不一样
		public static final String AUTHORITY = "com.jacp.provider.demo.programmer";
		public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY +"/programmers");
		public static final String TABLE_NAME = "programmer";
		public static final String DEFAULT_SORT_ORDER = "age desc";
		
		public static final String NAME = "name";
		public static final String AGE = "age";
		
	}
	
	/**
	 * 保存leader表中用到的常量
	 * @author mayliang
	 *
	 */
	public static final class LeaderColumns implements BaseColumns {
		public static final String AUTHORITY = "com.jacp.provider.demo.leader";
		public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY +"/leaders");
		public static final String TABLE_NAME = "leader";
		public static final String DEFAULT_SORT_ORDER = "level desc";
		
		public static final String NAME = "name";
		public static final String TITLE = "title";
		public static final String LEVEL = "level";
		
	}
	
}
对leader表进行增删改查操作的ContentProvider类:

LeaderProvider.java

package com.jacp.demo.provider;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

import com.jacp.database.DatabaseHelper;

/**
 * 对leader表进行操作的ContentProvider
 * @author jacp
 *
 */
public class LeaderProvider extends ContentProvider {

    private static HashMap sLeadersProjectionMap;

    private static final int LEADERS = 1;
    private static final int LEADER_ID = 2;

    private static final UriMatcher sUriMatcher;

    private DatabaseHelper mOpenHelper;

    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(Provider.LeaderColumns.TABLE_NAME);

        switch (sUriMatcher.match(uri)) {
        case LEADERS:
            qb.setProjectionMap(sLeadersProjectionMap);
            break;

        case LEADER_ID:
            qb.setProjectionMap(sLeadersProjectionMap);
            qb.appendWhere(Provider.LeaderColumns._ID + "=" + uri.getPathSegments().get(1));
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // If no sort order is specified use the default
        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = Provider.LeaderColumns.DEFAULT_SORT_ORDER;
        } else {
            orderBy = sortOrder;
        }

        // Get the database and run the query
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
        case LEADERS:
            return Provider.CONTENT_TYPE;
        case LEADER_ID:
            return Provider.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        // Validate the requested uri
        if (sUriMatcher.match(uri) != LEADERS) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        // Make sure that the fields are all set
        if (values.containsKey(Provider.LeaderColumns.NAME) == false) {
            values.put(Provider.LeaderColumns.NAME, "");
        }
        
        if (values.containsKey(Provider.LeaderColumns.TITLE) == false) {
        	values.put(Provider.LeaderColumns.TITLE, "");
        }

        if (values.containsKey(Provider.LeaderColumns.LEVEL) == false) {
            values.put(Provider.LeaderColumns.LEVEL, 0);
        }

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(Provider.LeaderColumns.TABLE_NAME, Provider.LeaderColumns.NAME, values);
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(Provider.LeaderColumns.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case LEADERS:
            count = db.delete(Provider.LeaderColumns.TABLE_NAME, where, whereArgs);
            break;

        case LEADER_ID:
            String noteId = uri.getPathSegments().get(1);
            count = db.delete(Provider.LeaderColumns.TABLE_NAME, Provider.LeaderColumns._ID + "=" + noteId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case LEADERS:
            count = db.update(Provider.LeaderColumns.TABLE_NAME, values, where, whereArgs);
            break;

        case LEADER_ID:
            String noteId = uri.getPathSegments().get(1);
            count = db.update(Provider.LeaderColumns.TABLE_NAME, values, Provider.LeaderColumns._ID + "=" + noteId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(Provider.LeaderColumns.AUTHORITY, "leaders", LEADERS);
        sUriMatcher.addURI(Provider.LeaderColumns.AUTHORITY, "leaders/#", LEADER_ID);

        sLeadersProjectionMap = new HashMap();
        sLeadersProjectionMap.put(Provider.LeaderColumns._ID, Provider.LeaderColumns._ID);
        sLeadersProjectionMap.put(Provider.LeaderColumns.NAME, Provider.LeaderColumns.NAME);
        sLeadersProjectionMap.put(Provider.LeaderColumns.TITLE, Provider.LeaderColumns.TITLE);
        sLeadersProjectionMap.put(Provider.LeaderColumns.LEVEL, Provider.LeaderColumns.LEVEL);
    }
}
对programmer表进行增删改查操作的ContentProvider类:

ProgrammerProvider.java

package com.jacp.demo.provider;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

import com.jacp.database.DatabaseHelper;

/**
 * 对programmer表进行操作的ContentProvider
 * @author jacp
 *
 */
public class ProgrammerProvider extends ContentProvider {

    private static HashMap sprogrammersProjectionMap;

    private static final int PROGRAMMERS = 1;
    private static final int PROGRAMMERS_ID = 2;

    private static final UriMatcher sUriMatcher;

    private DatabaseHelper mOpenHelper;

    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(Provider.ProgrammerColumns.TABLE_NAME);

        switch (sUriMatcher.match(uri)) {
        case PROGRAMMERS:
            qb.setProjectionMap(sprogrammersProjectionMap);
            break;

        case PROGRAMMERS_ID:
            qb.setProjectionMap(sprogrammersProjectionMap);
            qb.appendWhere(Provider.ProgrammerColumns._ID + "=" + uri.getPathSegments().get(1));
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // If no sort order is specified use the default
        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = Provider.ProgrammerColumns.DEFAULT_SORT_ORDER;
        } else {
            orderBy = sortOrder;
        }

        // Get the database and run the query
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
        case PROGRAMMERS:
            return Provider.CONTENT_TYPE;
        case PROGRAMMERS_ID:
            return Provider.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        // Validate the requested uri
        if (sUriMatcher.match(uri) != PROGRAMMERS) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        // Make sure that the fields are all set
        if (values.containsKey(Provider.ProgrammerColumns.NAME) == false) {
            values.put(Provider.ProgrammerColumns.NAME, "");
        }

        if (values.containsKey(Provider.ProgrammerColumns.AGE) == false) {
            values.put(Provider.ProgrammerColumns.AGE, 0);
        }

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(Provider.ProgrammerColumns.TABLE_NAME, Provider.ProgrammerColumns.NAME, values);
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(Provider.ProgrammerColumns.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case PROGRAMMERS:
            count = db.delete(Provider.ProgrammerColumns.TABLE_NAME, where, whereArgs);
            break;

        case PROGRAMMERS_ID:
            String noteId = uri.getPathSegments().get(1);
            count = db.delete(Provider.ProgrammerColumns.TABLE_NAME, Provider.ProgrammerColumns._ID + "=" + noteId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case PROGRAMMERS:
            count = db.update(Provider.ProgrammerColumns.TABLE_NAME, values, where, whereArgs);
            break;

        case PROGRAMMERS_ID:
            String noteId = uri.getPathSegments().get(1);
            count = db.update(Provider.ProgrammerColumns.TABLE_NAME, values, Provider.ProgrammerColumns._ID + "=" + noteId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(Provider.ProgrammerColumns.AUTHORITY, "programmers", PROGRAMMERS);
        sUriMatcher.addURI(Provider.ProgrammerColumns.AUTHORITY, "programmers/#", PROGRAMMERS_ID);

        sprogrammersProjectionMap = new HashMap();
        sprogrammersProjectionMap.put(Provider.ProgrammerColumns._ID, Provider.ProgrammerColumns._ID);
        sprogrammersProjectionMap.put(Provider.ProgrammerColumns.NAME, Provider.ProgrammerColumns.NAME);
        sprogrammersProjectionMap.put(Provider.ProgrammerColumns.AGE, Provider.ProgrammerColumns.AGE);
    }
}
保存leader表的数据对象:

Leader.java

package com.jacp.pojos;

public class Leader {

	public String name;
	public String title;
	public int level;
}
保存Programmer表的数据对象:

Programmer.java

package com.jacp.pojos;

public class Programmer {

	public String name;
	public int age;
}

Activity测试:

package com.jacp.demo;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.jacp.demo.provider.Provider;
import com.jacp.pojos.Leader;
import com.jacp.pojos.Programmer;

public class ContentProviderDemoActivity extends Activity {
	
	private static final String TAG = "ProviderActivity";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        testLeader();
        testProgrammer();
    }
    
    private void testProgrammer() {
    	Programmer p = new Programmer();
        p.name = "jacp";
        p.age = 99;
        int id = insertProgrammer(p);
        queryProgrammer(id);
    }
    
    private int insertProgrammer(Programmer programmer) {
    	ContentValues values = new ContentValues();
    	values.put(Provider.ProgrammerColumns.NAME, programmer.name);
    	values.put(Provider.ProgrammerColumns.AGE, programmer.age);
    	Uri uri = getContentResolver().insert(Provider.ProgrammerColumns.CONTENT_URI, values);
    	Log.i(TAG, "insert uri="+uri);
    	String lastPath = uri.getLastPathSegment();
    	if (TextUtils.isEmpty(lastPath)) {
    		Log.i(TAG, "insert failure!");
    	} else {
    		Log.i(TAG, "insert success! the id is " + lastPath);
    	}
    	
    	return Integer.parseInt(lastPath);
    }
    
    private void queryProgrammer(int id) {
    	Cursor c = getContentResolver().query(Provider.ProgrammerColumns.CONTENT_URI, new String[] { Provider.ProgrammerColumns.NAME, Provider.ProgrammerColumns.AGE }, Provider.ProgrammerColumns._ID + "=?", new String[] { id + "" }, null);
    	if (c != null && c.moveToFirst()) {
    		Programmer p = new Programmer();
    		p.name = c.getString(c.getColumnIndexOrThrow(Provider.ProgrammerColumns.NAME));
    		p.age = c.getInt(c.getColumnIndexOrThrow(Provider.ProgrammerColumns.AGE));
    		c.close(); // 用完Cursor要释放资源,如果Cursor没有关闭系统会打出Error级别的Log
    		Log.i(TAG, "programmer.name="+p.name+"---programmer.age="+p.age);
    	} else {
    		Log.i(TAG, "query failure!");
    	}
    }
    
    private void testLeader() {
    	Leader leader = new Leader();
    	leader.name = "jacky";
    	leader.title = "CTO";
    	leader.level = 30;
    	int id = insertLeader(leader);
    	queryLeader(id);
    }
    
    private int insertLeader(Leader leader) {
    	ContentValues values = new ContentValues();
    	values.put(Provider.LeaderColumns.NAME, leader.name);
    	values.put(Provider.LeaderColumns.TITLE, leader.title);
    	values.put(Provider.LeaderColumns.LEVEL, leader.level);
    	Uri uri = getContentResolver().insert(Provider.LeaderColumns.CONTENT_URI, values);
    	Log.i(TAG, "insert uri="+uri);
    	String lastPath = uri.getLastPathSegment();
    	if (TextUtils.isEmpty(lastPath)) {
    		Log.i(TAG, "insert failure!");
    	} else {
    		Log.i(TAG, "insert success! the id is " + lastPath);
    	}
    	
    	return Integer.parseInt(lastPath);
    }
    
    private void queryLeader(int id) {
    	Cursor c = getContentResolver().query(Provider.LeaderColumns.CONTENT_URI, new String[] { Provider.LeaderColumns.NAME, Provider.LeaderColumns.TITLE, Provider.LeaderColumns.LEVEL }, Provider.LeaderColumns._ID + "=?", new String[] { id + "" }, null);
    	if (c != null && c.moveToFirst()) {
    		Leader leader = new Leader();
    		leader.name = c.getString(c.getColumnIndexOrThrow(Provider.LeaderColumns.NAME));
    		leader.title = c.getString(c.getColumnIndexOrThrow(Provider.LeaderColumns.TITLE));
    		leader.level = c.getInt(c.getColumnIndexOrThrow(Provider.LeaderColumns.LEVEL));
    		Log.i(TAG, "leader.name="+leader.name+"---leader.title="+leader.title+"---leader.level="+leader.level);
    	} else {
    		Log.i(TAG, "query failure!");
    	}
    }
}

Manifest.xml文件中的注册(要注意的地方,下面已标注):




    

    
        
            
                

                
            
        
        
        
        
        
        
    

demo下载地址: http://download.csdn.net/detail/maylian7700/4150144

如有遗漏不当之处,欢迎批评指正!

你可能感兴趣的:(android技巧总结)