1
package com.content.provider;
import android.app.Activity;
import android.os.Bundle;
public class Android_contentProviderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2
package com.content.provider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
private static String name="mydb.db";
private static int version=1;
public DbHelper(Context context) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
String sql="create table student(id integer primary key autoincrement,name varchar(64),address varchar(64))";
database.execSQL(sql);//对数据库表的创建
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
package com.content.provider;
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.net.Uri;
import android.util.Log;
public class StudentProvider extends ContentProvider {
private final String TAG = "StudentProvider";
private DbHelper helper = null;
private static final UriMatcher URI_MATCHER = new UriMatcher(
UriMatcher.NO_MATCH);
private static final int STUDENT = 1;// 操作单条记录
private static final int STUDENTS = 2;// 操作多次记录
static {
URI_MATCHER.addURI("com.content.provider.StudentProvider", "student",
STUDENT);
URI_MATCHER.addURI("com.content.provider.StudentProvider/#", "student", STUDENTS);
}
public StudentProvider() {
// TODO Auto-generated constructor stub
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
int flag=URI_MATCHER.match(uri);
switch(flag){
case STUDENT:
return "vnd.android.cursor.item/student";
case STUDENTS:
return "vnd.android.cursor.dir/student";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri resultUri=null;
int flag=URI_MATCHER.match(uri);
switch(flag){
case STUDENT:
SQLiteDatabase database=helper.getWritableDatabase();
long id=database.insert("student", null, values);
resultUri=ContentUris.withAppendedId(uri, id);
break;
}
Log.i(TAG, "--->"+resultUri.toString());
return resultUri;
}
@Override
public boolean onCreate() {
helper=new DbHelper(getContext());
return false;
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}
package com.content.provider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase {
public Test() {
// TODO Auto-generated constructor stub
}
public void insert(){
//需要一个内容解析者
ContentResolver contentResolver=getContext().getContentResolver();
Uri url=Uri.parse("content://com.content.provider.StudentProvider/student");
ContentValues values=new ContentValues();
values.put("name", "张三");
values.put("name", "李四");
contentResolver.insert(url, values);
}
}