这里实现的是前者,第一个ContentProvider
一、首先这里创建一个类继承ContentProvider,然后重写里面的方法
public class MyContentProvider extends ContentProvider{
private MySqliter sqliter;
@Override
public boolean onCreate() {
sqliter = new MySqliter(getContext());
return false;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = sqliter.getWritableDatabase();
db.insert("person", null, values);
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqliter.getWritableDatabase();
db.delete("person", selection, selectionArgs);
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = sqliter.getWritableDatabase();
db.update("person", values, selection, selectionArgs);
return 0;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = sqliter.getReadableDatabase();
Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
}
二、创建数据库以及表
public class MySqliter extends SQLiteOpenHelper{
public MySqliter(Context context) {
super(context, "system.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person (_id integer primary key autoincrement,name varchar(20),age integer)");
db.execSQL("insert into person (name,age) values (?,?)",new Object[]{"皮皮虎",20});
db.execSQL("insert into person (name,age) values (?,?)",new Object[]{"皮皮虾",25});
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
三、需要在清单文件中配置
<!-- 可调用权限 exported="true" -->
<provider
android:exported="true"
android:name="com.example.day5_contentprovider.MyContentProvider"
android:authorities="com.example.day5_contentprovider"></provider>
这里实现的是后者,第二个ContentProvider,它是通过调用前者来实现的需求
四、注意一点 URL是写个上一个工程XML中的android:authorities=”com.example.day5_contentprovider”>
public class MainActivity extends Activity {
private ContentResolver resolver;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取内容解析者
resolver = getContentResolver();
//uri 同意资源定位符
//获取URi
uri = Uri.parse("content://com.example.day5_contentprovider");
}
public void onAdd(View v) {
ContentValues values = new ContentValues();
values.put("name", "黄辉冯");
values.put("age", 33);
resolver.insert(uri, values );
}
public void onDelete(View v) {
resolver.delete(uri, "name = ?", new String[]{"皮皮虎"});
}
public void onUpdate(View v) {
ContentValues values = new ContentValues();
values.put("name", "皮皮虾");
values.put("age", 38);
resolver.update(uri, values , "name = ?", new String[]{"他大舅"});
}
public void onQuery(View v) {
//projection 查询的列
Cursor cursor = resolver.query(uri, null, null, null, "age asc");
//while遍历
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.i("---name"+name, "age"+age);
}
}
}