博主 天每自学Android,今天研究到了ContentProvider 和 ContentResolver 。
ContentProvider 为四大基本组键之一,与其他组键用法差不多,首先都要继承一个类,然后重写方法,最后就是在清单文件中配置,具体要怎么做呢? 以查询和修改为例,咱们看代码,博主用的是SQLite数据库保存数据做的模拟底层的ContentProvider,先自己存储在查找出来,然后再在ContentResolver修改
先看内容提供者的
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase db;
private UriMatcher uriMatcher;
@Override
public boolean onCreate() {
Log.i("test", "onCreate");
DbUtil dbUtil = new DbUtil(getContext(), "G150820.db", null, 2);
db = dbUtil.getReadableDatabase();
//实例化URI匹配器
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//添加规则
//01.查询所有 content://com.example.contentprovider.PERSON/student
uriMatcher.addURI("com.example.contentprovider.PERSON", "student", 1);
//02.查询单个 content://com.example.contentprovider.PERSON/student/3
uriMatcher.addURI("com.example.contentprovider.PERSON", "student/#", 2);
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
//根据URI匹配器开始匹配Uri
int code = uriMatcher.match(uri);
switch (code) {
case 1:
//查询所有
Log.i("test", "查询所有");
//获取数据库中所有的数据
return db.query(false, "student", strings, s, strings1, null, null, s1, null);
case 2:
//查询单个
//获取# 的值
long id = ContentUris.parseId(uri);
Log.i("test", "查询单个");
return db.rawQuery("select * from student where _id=?", new String[]{id + ""});
}
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
Log.i("test", "getType");
return null;
}
//添加
@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
Log.i("test", "insert");
return null;
}
//删除
@Override
public int delete(Uri uri, String s, String[] strings) {
Log.i("test", "delete");
return 0;
}
//修改
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
Log.i("test", "update"+contentValues+s+strings);
return db.update("student",contentValues,s,strings);
}
}
自己写了一个数据库帮助类
//数据库帮助类
public class DbUtil extends SQLiteOpenHelper {
//context 上下文,
// 数据库名字(XXX.db)
// 文件名 factory
// version 数据库版本
public DbUtil(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//创建表的操作
//调用一次
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建表
sqLiteDatabase.execSQL("create table student(_id integer primary key autoincrement,name,age)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MainActivity的布局文件
<EditText
android:id="@+id/et_main_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID:" />
<EditText
android:id="@+id/et_main_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name:" />
<EditText
android:id="@+id/et_main_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="age:" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="save"
android:text="保存" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="query"
android:text="查询" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_mainss_list"
></ListView>
</LinearLayout>
然后是MainActivity的代码
public class MainActivity extends AppCompatActivity {
private Cursor cursor;
private SQLiteDatabase sqLiteDatabase;
private EditText et_main_id;
private EditText et_main_name;
private EditText et_main_age;
private SimpleCursorAdapter simpleCursorAdapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_id = (EditText) findViewById(R.id.et_main_id);
et_main_name = (EditText) findViewById(R.id.et_main_name);
et_main_age = (EditText) findViewById(R.id.et_main_age);
listView = (ListView) findViewById(R.id.lv_mainss_list);
//创建数据库
DbUtil dbUtil = new DbUtil(this, "G150820.db", null, 2);
sqLiteDatabase = dbUtil.getReadableDatabase();
queryAll();
//设置适配器
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_list, 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.setAdapter(simpleCursorAdapter);
}
public void save(View view) {
String name = et_main_name.getText().toString();
String age = et_main_age.getText().toString();
//保存到数据库
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
values.putNull("_id");
sqLiteDatabase.insert("student", "name", values);
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
public void query(View view) {
if (TextUtils.isEmpty(et_main_name.getText())) {
queryAll();
} else {
queryAll(et_main_name.getText().toString());
}
simpleCursorAdapter.changeCursor(cursor);
}
public void queryAll(String... str) {
String name = null;
int length = str.length;
if (length == 0) {
Toast.makeText(MainActivity.this, "查询所有", Toast.LENGTH_SHORT).show();
cursor = sqLiteDatabase.rawQuery("select * from student limit ?,?", new String[]{2 + "", 2 + ""});
} else {
name = str[0];
Toast.makeText(MainActivity.this, "模糊查询:" + name, Toast.LENGTH_SHORT).show();
cursor = sqLiteDatabase.rawQuery("select * from student where name like ?", new String[]{"%" + name + "%"});
}
}
}
自己给ListView定义一个布局
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/tv_item_list_id"
android:layout_weight="1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/tv_item_list_name"
android:layout_weight="1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/tv_item_list_age"
android:layout_weight="1"
/>
别忘了清单文件中还要配置好 <!--配置内容提供者-->
<!--配置内容提供者-->
<provider
android:authorities="com.example.contentprovider.PERSON"
android:name="com.contentProvider.MyContentProvider"
android:exported="true"
></provider>
对于ContentResolver的要求并没有那么高如果大家想看ContentResolver中的内容关注博主,查看大大的博客哟!
博主第一次写博客 大家有什么好的意见可以和博主说说 感谢各位的一紧