Android(安卓)Studio 的初步使用,ContentProvider创建
16lz
2021-01-26
Android Studio 的教程文章链接
http://www.open-open.com/lib/view/open1468121363300.html
内容提供者可以把自己的数据库暴露给别人的应用程序访问。
*创建一个类 Provider extends 继承 ContentProvider
*在清单文件.xml里面配置 内容提供者。配置完整的路径和主机名。
android:name="cn.itcast.contentprovider.PersonDBProvider"
android:authorities="cn.itcast.contentprovider.personprovider"
*在Provider类中定义出来一些数据操作的uri
利用uriMarcher的方法添加一些指定的特殊路径
matcher.addURI("cn.itcast.contentprovider.personprovider", "insert", 1);matcher.addURI("cn.itcast.contentprovider.personprovider", "delete", 2);matcher.addURI("cn.itcast.contentprovider.personprovider", "update", 3);matcher.addURI("cn.itcast.contentprovider.personprovider", "query", 4);
*实现Provider增删该查的方法。
***
使用内容提供者查询数据
*获取内容提供者的解析器
ContentResolver resolver=getContentResolver();
*使用resolver进行增删该查的操作。
Cursor cursor = contentResolver.query(uri, null, null, null, null);
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String number = cursor.getString(cursor.getColumnIndex("number"));
Person p = new Person(id, name, number);
persons.add(p);
}
cursor.close();
转载于:https://www.cnblogs.com/yboli/p/5663547.html
更多相关文章
- Android(安卓): CardView的优雅使用
- activity之简单的页面跳转
- Android内容提供者之读取联系人信息
- 在EditText中限制输入内容的长度
- android TextView实现自动滚动和Java中改变显示内容
- android调用系统短信Intent时将预填接收号码和内容
- android 动态修改menu item的内容title
- Android之Google Map(二)
- [Android]simplelistadapter 与数据库绑定显示list 并且根据数据