ContentProvider(内容提供者)和ContentResolve(内容访问者)
来源:互联网 发布:游戏编程图书 编辑:程序博客网 时间:2024/05/18 02:03
ContentProvder(内容提供者)是安卓四大组件之一,用来共享数据应用程序的数据,当你需要把你的应用程序的数据共享给其他的应用程序的时候,才需要它。它对外提供了其它应用可以直接访问的增删改查方法。
下面是实现ContentProvider的方法:1.写一个查看界面:
<LinearLayout xmlns:android="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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.smz.g150831_android16_sqlite.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_id" android:hint="ID:" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_name" android:hint="Name:" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_age" android:hint="age:" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" android:onClick="save" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/list" ></ListView></LinearLayout>
2.首先在数据提供者的应用里创建一个类(MyProvider),继承ContentProvider ,并实现四个方法,
然后在androidMainfest.xml 文件中去配置这个ContentProvider 子类
<!--配置内容提供者--> <provider android:authorities="com.example.smz.g150831_android16_sqlite.person" android:name="com.example.smz.g150831_android16_sqlite.MyProvider" android:exported="true" ></provider>3.如下图所示新建一个文件夹
然后写一个DbHelper的类,链接数据库
public class DbHelper extends SQLiteOpenHelper { /** * * @param context 上下文 * @param name 名字(数据库名),文件名 * @param factory 游标工厂,多数情况:null * @param version 数据库版本 */ public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); Log.i("test","构造"); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { //操作:创建表的操作 Log.i("test","创建表"); sqLiteDatabase.execSQL("create table person(_id integer primary key autoincrement,name,age)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { Log.i("test","数据库版本,低--->高"); }}4.在MyProvider这个类中
//实例化URI匹配器(uriMatcher是用来分辨查询的内容的)然后添加匹配规则,详见如下代码:
public class MyProvider extends ContentProvider { private SQLiteDatabase sqLiteDatabase; private UriMatcher uriMatcher; private static final int PERSONS=1; private static final int PERSON=2; @Override public boolean onCreate() { DbHelper dbHelper=new DbHelper(getContext(),"G150831.db",null,2); sqLiteDatabase = dbHelper.getReadableDatabase(); //实例化URI匹配器 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); //添加匹配规则 uriMatcher.addURI("com.example.smz.g150831_android16_sqlite.person","persons",MyProvider.PERSONS); uriMatcher.addURI("com.example.smz.g150831_android16_sqlite.person","persons/#",MyProvider.PERSON); Log.i("tt","onCreate"); return false; } @Nullable @Override public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) { //查询所有的数据 Log.i("tt","query"); int code=uriMatcher.match(uri); switch (code) { case MyProvider.PERSONS: Log.i("tt","query查询所有"); return sqLiteDatabase.query(true,"person",strings,s,strings1,null,null,s1,null); case MyProvider.PERSON: Log.i("tt","query查询单个"); //获取#好的值 long id= ContentUris.parseId(uri); return sqLiteDatabase.rawQuery("select * from person where _id=?",new String[]{id+""}); } return null; } @Nullable @Override public String getType(Uri uri) { Log.i("tt","getType"); return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues contentValues) { Log.i("tt","insert"); return null; } @Override public int delete(Uri uri, String s, String[] strings) { Log.i("tt","delete"); return 0; } @Override public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { Log.i("tt","update"); return 0; }}
内容访问者ContentResolver
ContentResolver是通过URI来查询ContentProvider中提供的数据。除了URI以外,还必须知道需要获取的数据段的名称,以及此数据段的数据类型。
再新建一个项目去访问者内容提供者里的数据,
1.写一个获取数据的界面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.smz.g150831_android24_contentresolver.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取数据" android:onClick="getdata" /></LinearLayout>2.获取内容访问者
public class MainActivity extends AppCompatActivity { private ContentResolver cr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取内容访问者 cr = getContentResolver(); } }用cr来写查询的操作:(具体实现代码如下)
InstanceState); setContentView(R.layout.activity_main); //获取内容访问者 cr = getContentResolver(); et_main = (EditText) findViewById(R.id.et_main); } public void getdata(View view) { if (TextUtils.isEmpty(et_main.getText())) { //如果查询为空就查所有 Log.i("tt","查所有"); uri = Uri.parse("content://com.example.smz.g150831_android16_sqlite.person/persons"); } else { //否则查询单个 Log.i("tt","查询单个"); String id = et_main.getText().toString(); uri = Uri.parse("content://com.example.smz.g150831_android16_sqlite.person/persons/" + id); } Cursor cursor = cr.query(uri, null, null, null, null); //单个查询 while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.i("tt", "_" + id + "_" + name + "_" + age); } }}
0 0
- ContentProvider(内容提供者)和ContentResolve(内容访问者)
- ContentProvider(内容提供者)和ContentResolve(内容访问者)
- ContentProvider内容提供者 和 ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentResolve内容访问者(访问系统)
- Android ContentProvider(内容提供者 )+ContentResolver(内容访问者)
- ContentProvider(内容提供者)与ContentResolver(内容访问者)
- ContentProvider(内容提供者)
- ContentProvider(内容提供者)
- Android 中ContentProvider内容提供者和ContentResolver内容访问者
- Android:内容提供者(ContentProvider)和内容访问者(ContentResolver)
- ContentProvider内容提供者与ContentResolver内容访问者
- Android----ContentProvider(内容提供者) ContentResolver(内容访问者)
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- 内容提供者(ContentProvider)复习
- 最长上升连续子序列
- 时间序列分析笔记(待整理)
- js向div中追加html代码
- 数据结构与算法分析笔记与总结(java实现)--二叉树4:二叉树的序列化和反序列化练习题
- MongoDB笔记
- ContentProvider(内容提供者)和ContentResolve(内容访问者)
- nginx 参数优化
- hi3519v101 sdk 编译错误
- 友盟推送Python demo失败
- Java设计模式
- 动画——Interpolator插值器
- 类模板和函数模板
- java.lang.NullPointerException: Attempt to invoke virtual method 'void com.hhl.library.FlowTagLayout
- 单页WEB应用(三),Chat聊天模块