3.StudentColumns.java package cn.gd.pl.lan.providersample; import android.net.Uri; import android.provider.BaseColumns; public class StudentColumns implements BaseColumns{ public static final String AUTHORITY = "cn.gd.pl.lan.provider"; public static final int ALL = 1; public static final int SINGAL = 2; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/students/"); public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.lan.student"; public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.lan.student"; public static final String DATABASE = "student.db"; public static final int DB_VERSION = 1; public static final String TABLE = "students"; public static final String ID = "id"; public static final String NAME = "name"; public static final String AGE = "age"; public static final String SEX = "sex"; public static final String GRADE = "grade"; public static final String[] OUTPUT_COLUMNS = {"id","name","age","sex","grade"}; } 4.DBHelper.java package cn.gd.pl.lan.providersample; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context, StudentColumns.DATABASE, null, StudentColumns.DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("loki: before table create"); String sql = "create table " + StudentColumns.TABLE + "(" + StudentColumns.ID + " integer primary key," + StudentColumns.NAME + " text," + StudentColumns.AGE + " integer," + StudentColumns.SEX + " text," + StudentColumns.GRADE + " integer);"; db.execSQL(sql); System.out.println("loki:table create success :"+StudentColumns.TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { String sql = "drop table if exists "+ StudentColumns.TABLE; db.execSQL(sql); onCreate(db); } } 5.StudentProvider.java package cn.gd.pl.lan.providersample; 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.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; public class StudentProvider extends ContentProvider { private DBHelper helper; private UriMatcher matcher; @Override public int delete(Uri uri, String where, String[] whereArgs) { SQLiteDatabase db = helper.getWritableDatabase(); int count; switch(matcher.match(uri)){ case StudentColumns.ALL: count = db.delete(StudentColumns.TABLE, where, whereArgs); break; case StudentColumns.SINGAL: String rowID = uri.getPathSegments().get(1); count = db.delete(StudentColumns.TABLE, StudentColumns.ID + "=" + rowID + (!TextUtils.isEmpty(where)?" AND (" + where + ")" :""), whereArgs); break; default: throw new IllegalArgumentException("Unknown Uri :"+uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public String getType(Uri uri) { switch(matcher.match(uri)){ case StudentColumns.ALL: return StudentColumns.CONTENT_TYPE; case StudentColumns.SINGAL: return StudentColumns.CONTENT_ITEM_TYPE; } throw new IllegalArgumentException("Unknown Uri :"+uri); } @Override public Uri insert(Uri uri, ContentValues cv) { SQLiteDatabase db = helper.getWritableDatabase(); long rowID = 0; switch(matcher.match(uri)){ case StudentColumns.ALL: rowID = db.insert(StudentColumns.TABLE, null, cv); if(rowID > 0){ System.out.println("loki:insert row success!"); Uri insertedUri = ContentUris.withAppendedId(uri, rowID); getContext().getContentResolver().notifyChange(insertedUri, null); return insertedUri; } } throw new SQLiteException(); } @Override public boolean onCreate() { helper = new DBHelper(this.getContext()); matcher = new UriMatcher(UriMatcher.NO_MATCH); matcher.addURI(StudentColumns.AUTHORITY, StudentColumns.TABLE, StudentColumns.ALL); matcher.addURI(StudentColumns.AUTHORITY, StudentColumns.TABLE+"/#", StudentColumns.SINGAL); System.out.println("loki:provider onCreate"); return true; } @Override public Cursor query(Uri uri, String[] outputColumns, String where, String[] whereArgs, String orderBy) { SQLiteQueryBuilder sqb = new SQLiteQueryBuilder(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor; System.out.println("loki:"+matcher.match(uri)); HashMap columnMap = new HashMap(); columnMap.put(StudentColumns.ID, StudentColumns.ID); columnMap.put(StudentColumns.NAME, StudentColumns.NAME); columnMap.put(StudentColumns.AGE, StudentColumns.AGE); columnMap.put(StudentColumns.SEX, StudentColumns.SEX); columnMap.put(StudentColumns.GRADE, StudentColumns.GRADE); switch(matcher.match(uri)){ case StudentColumns.ALL: sqb.setTables(StudentColumns.TABLE); sqb.setProjectionMap(columnMap); break; case StudentColumns.SINGAL: sqb.setTables(StudentColumns.TABLE); sqb.setProjectionMap(columnMap); String rowID = uri.getPathSegments().get(1); sqb.appendWhere(StudentColumns.ID + "=" + rowID); System.out.println("loki:rowID = " + rowID); break; default: throw new IllegalArgumentException("Unknown Uri :"+uri); } if(TextUtils.isEmpty(orderBy)) orderBy = "id DESC"; cursor = sqb.query(db, outputColumns, where, whereArgs, null, null, orderBy); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; } @Override public int update(Uri uri, ContentValues cv, String where, String[] whereArgs) { SQLiteDatabase db = helper.getWritableDatabase(); int count; switch(matcher.match(uri)){ case StudentColumns.ALL: count = db.update(StudentColumns.TABLE, cv, where, whereArgs); break; case StudentColumns.SINGAL: String rowID = uri.getPathSegments().get(1); count = db.update(StudentColumns.TABLE, cv,StudentColumns.ID + "=" + rowID +(!TextUtils.isEmpty(where)? " AND (" + where + ")" :""), whereArgs); break; default: throw new IllegalArgumentException("Unknown Uri :"+uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } } 6.activity_main.xml 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:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" tools:context=".MainActivity" /> 二、项目【TestProvider】 1.TestActivity.java package cn.example.testprovider; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.widget.TextView; public class TestActivity extends Activity { private TextView tv; private ContentResolver cr; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); tv = (TextView) findViewById(R.id.tv); cr = this.getContentResolver(); String[] projection = {"id","name","age","sex","grade"}; Cursor cursor = cr.query(Uri.parse("content://cn.gd.pl.lan.provider/students/"), projection , null, null, null); cursor.moveToFirst(); CharSequence rs = "result:"; while(cursor.moveToNext()){ rs = rs + " ,id=" + cursor.getInt(0)+"."; rs = rs + " ,name=" + cursor.getString(1)+"."; rs = rs + " ,age=" + cursor.getInt(2)+"."; rs = rs + " ,sex=" + cursor.getString(3)+"."; rs = rs + " ,grade=" + cursor.getInt(4)+"."; } System.out.println(rs); tv.setText(rs); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_test, menu); return true; } } 2.activity_test.xml 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:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" tools:context=".TestActivity" /> --------------------------------华丽分割线----------------------------------- 心得与体会: 细细口味,慢慢研究,cursor.getXxx(int sss);参数(表列数)是从0开始数的,这个要特别注意,其他注意点都加红色了。