简介 主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性。
备注:以下demo实现了一套简单快捷的内容者实现对数据的增删改查,截取部分代码,加解析。
—–》第一个应用程序“内容提供者“”demo创建工程截图:
—–》Activity代码 package com.example.android_sqlite;
import android.app.ListActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.Toast;
import com.zking.db.DbHelper;
public class MainActivity extends ListActivity {
PRivate ListView listView;private SQLiteDatabase sdb;private SimpleCursorAdapter simpleCursorAdapter;private Cursor cursor;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化DbHelper DbHelper db = new DbHelper(this, "person.db", null, 1); //加数据 sdb = db.getWritableDatabase(); for (int i = 0; i < 10; i++) { ContentValues contentvalues = new ContentValues(); contentvalues.put("name", "张三" + i); contentvalues.put("age", i); contentvalues.putNull("_id"); sdb.insert("person","name",contentvalues); } Toast.makeText(MainActivity.this, "数据加载完毕!", Toast.LENGTH_SHORT).show(); //获取SQLiteDatabase //获取ListView listView = getListView(); sdb = db.getReadableDatabase(); //查询所有 cursor = sdb.query(false, "person", null, null, null, null, null, null, null); //循环游标,放值,设置适配器 simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_listview, cursor, new String[]{"_id", "name", "age"}, new int[]{R.id.tv_item_list_id, R.id.tv_item_list_name, R.id.tv_item_list_age}); //给listView设置适配器 listView.setAdapter(simpleCursorAdapter);}public Cursor queryAllPerson(){ Cursor cursor = sdb.rawQuery("select * from person",null); return cursor;}public void query(View view){ cursor=queryAllPerson(); //通知适配器发生改变 simpleCursorAdapter.changeCursor(cursor);}}
—–>MyContentProvider代码 package com.example.android_sqlite;
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.support.annotation.Nullable;
import com.zking.db.DbHelper;
/** * Created by Administrator on 2017/2/6. */ public class MyContentProvider extends ContentProvider {
private DbHelper dbHelper;private SQLiteDatabase sqLiteDatabase;// uri匹配相关private static UriMatcher uriMatcher;// 主机名称(这一部分是可以随便取得)private static final String authority = "com.example.android_sqlite.PERSON";// 注册该内容提供者匹配的uristatic { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(authority, "person", 1);// 代表当前表中的所有的记录 uriMatcher.addURI(authority, "person/#", 2);// 代表当前表中的某条特定的记录,记录id便是#处得数字}@Overridepublic boolean onCreate() { dbHelper = new DbHelper(getContext(), "person.db", null, 1); sqLiteDatabase = dbHelper.getReadableDatabase(); return true;}@Nullable@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { int code = uriMatcher.match(uri); switch (code) { case 1: Cursor cursor = sqLiteDatabase.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; case 2: // 从uri中解析出ID long id = ContentUris.parseId(uri); Cursor cursor2 = sqLiteDatabase.query("person", projection, "_id=?", new String[]{id + ""}, null, null, sortOrder); return cursor2; } return null;}@Nullable@Overridepublic String getType(Uri uri) { return null;}@Nullable@Overridepublic Uri insert(Uri uri, ContentValues values) { sqLiteDatabase.insert("person", null, values); return uri;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) { return sqLiteDatabase.delete("person",selection,selectionArgs);}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return sqLiteDatabase.update("person",values,selection,selectionArgs);}}
——》main.xml为默认。
—–》第二个应用程序“内容监听者”创建工程截图:
——>MainActivity代码 package com.example.android_contentprovider_sqlite;
import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ContentResolver contentResolver;private Button bt_main_add;private Button bt_main_query;private Button bt_main_delete;private Button bt_main_update;private EditText et_main_query;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contentResolver = this.getContentResolver(); //查询 bt_main_query = (Button) findViewById(R.id.bt_main_query); et_main_query = (EditText) findViewById(R.id.et_main_query); bt_main_query.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { query(v); } }); //添加 bt_main_add = (Button) findViewById(R.id.bt_main_add); bt_main_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { add(v); } }); //删除 bt_main_delete = (Button) findViewById(R.id.bt_main_delete); bt_main_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { delete(v); } }); //修改 bt_main_update = (Button) findViewById(R.id.bt_main_update); bt_main_update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { update(v); } });}//*********************************************************************************//查询public void query(View view) { Uri uri; //判断输入框是否为空 if (TextUtils.isEmpty(et_main_query.getText().toString())) { uri = Uri.parse("content://com.example.android_sqlite.PERSON/person"); } else { //查询单个 String id = et_main_query.getText().toString(); uri = Uri.parse("content://com.example.android_sqlite.PERSON/person/" + id); } //访问数据 Cursor cursor = contentResolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { int pid = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.i("test", pid + " " + name + " " + age); }}//添加public void add(View view) { ContentValues contentValues = new ContentValues(); contentValues.put("name", "大七"); contentValues.put("age", 13); Uri uri = Uri.parse("content://com.example.android_sqlite.PERSON"); contentResolver.insert(uri, contentValues); Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();}//删除public void delete(View view) { Uri uri = Uri.parse("content://com.example.android_sqlite.PERSON"); contentResolver.delete(uri, "_id=?", new String[]{6 + ""}); Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();}//修改public void update(View view) { Uri uri = Uri.parse("content://com.example.android_sqlite.PERSON"); ContentValues values = new ContentValues(); values.put("name", "大大"); values.put("age", 2); contentResolver.update(uri, values, "_id=?", new String[]{9 + ""}); Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();}}
新闻热点
疑难解答