自定义ContentProvider
来源:互联网 发布:插上网线后未识别网络 编辑:程序博客网 时间:2024/05/06 04:35
1.自定义Mycontent继承ContentProvider
public class Mycontent extends ContentProvider{ private MySqlite helper; static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH); static{ matcher.addURI("com.example.china","fatheradd", 0); matcher.addURI("com.example.china","fatherdel", 1); matcher.addURI("com.example.china","fatherup", 2); matcher.addURI("com.example.china","fathersel", 3); matcher.addURI("com.example.china","sonadd",4); matcher.addURI("com.example.china","sondel",5); matcher.addURI("com.example.china","sonup",6); matcher.addURI("com.example.china","sonsel",7); } @Override public boolean onCreate() { helper = new MySqlite(getContext()); helper.getWritableDatabase(); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = helper.getWritableDatabase(); Cursor cursor=null; switch (matcher.match(uri)) { case 3: cursor = db.query("father", projection, selection, selectionArgs, null, null, null); break; case 7: cursor = db.query("son", projection, selection, selectionArgs, null, null, null); break; default: break; } return cursor; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = helper.getWritableDatabase(); switch (matcher.match(uri)) { case 0: db.insert("father",null, values); break; case 4: db.insert("son",null, values); break; default: break; } return uri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = helper.getWritableDatabase(); switch (matcher.match(uri)) { case 1: db.delete("father", selection, selectionArgs); break; case 5: db.delete("son", selection, selectionArgs); break; default: break; } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; }
//配置清单文件
<provider android:name="com.example.content123.Mycontent" android:exported="true" android:authorities="com.example.china"> </provider>
//创建数据库兵并初始化几条数据
public class MySqlite extends SQLiteOpenHelper { public MySqlite(Context context) { super(context, "shiyou.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table father(_id integer primary key autoincrement,name varchar(20),age integer)"); db.execSQL("create table son(_id integer primary key autoincrement,sex varchar(20),phone varchar(11),uid integer,foreign key(uid) references father(_id))"); db.execSQL("insert into father(name,age) values('zhangsan',21)"); db.execSQL("insert into father(name,age) values('lisi',22)"); db.execSQL("insert into father(name,age) values('wangwu',25)"); db.execSQL("insert into son(sex,phone,uid) values('男','131455788',1)"); db.execSQL("insert into son(sex,phone,uid) values('女','111111111',2)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
内容提供之创建完毕
通过ContentResolver进行增、删、改、查
public class MainActivity extends Activity implements OnClickListener {
private EditText name;private EditText age;private EditText sex;private EditText num;private Button add;private Button del;private Button up;private Button sel;private ContentResolver contentResolver;private EditText ui;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); contentResolver = getContentResolver();}// 初始化控件信息private void init() { name = (EditText) findViewById(R.id.name); age = (EditText) findViewById(R.id.age); sex = (EditText) findViewById(R.id.sex); num = (EditText) findViewById(R.id.num); ui = (EditText) findViewById(R.id.ui); add = (Button) findViewById(R.id.add); del = (Button) findViewById(R.id.del); up = (Button) findViewById(R.id.up); sel = (Button) findViewById(R.id.sel); // 绑定监听 add.setOnClickListener(this); del.setOnClickListener(this); up.setOnClickListener(this); sel.setOnClickListener(this);}@Overridepublic void onClick(View v) { String uid = ui.getText().toString(); String ename = name.getText().toString(); String eage = age.getText().toString(); String esex = sex.getText().toString(); String pnum = num.getText().toString(); ContentValues values = new ContentValues(); switch (v.getId()) { case R.id.add: values.put("sex", esex); values.put("phone", pnum); values.put("uid", Integer.parseInt(uid)); contentResolver.insert( Uri.parse("content://com.example.china/sonadd"), values); break; case R.id.del: contentResolver.delete( Uri.parse("content://com.example.china/fatherdel"), "name=?", new String[] { ename }); break; case R.id.up: //contentResolver.update("content://com.example.china/sonup", values, where, selectionArgs) break; case R.id.sel: break; default: break; }}
}
0 0
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义contentprovider
- 自定义ContentProvider
- 自定义contentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- 自定义ContentProvider
- C#控制台基础 为什么存在部分类 partial
- 几个经常用到的字符串的截取
- ios 利用NSStream模拟请求http数据
- uva 1262 Password
- HTML 学习---字体与格式
- 自定义ContentProvider
- 状态机
- 玩转Android之在线视频播放控件Vitamio的使用
- hive踩过的小坑
- 背包问题 动态规划
- 统计函数与分组查询
- unicode下字符类型转换记录
- linux中的哲学思想
- linux常用命令(二)