首页 > 学院 > 开发设计 > 正文

ContentProvider及ContentResolver示例

2019-11-09 18:21:02
字体:
来源:转载
供稿:网友

首先要有一个ContentPRovider的demo

这里我自己定义了一个类  里面提供了一个Person表,用于下面的ContentResolver去获取它里面的数据

public class PersonsProvider extends ContentProvider {    private SQLiteDatabase sqLiteDatabase;    //重写方法    @Override    public boolean onCreate() {        //SQLiteOpenHelper        DbHelper dbHelper=new DbHelper(getContext(),"person.db",null,2);        //获取sqLiteDatabase对象        sqLiteDatabase = dbHelper.getReadableDatabase();        return false;    }    @Nullable    @Override    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {        Log.i("test","query");        Cursor c=null;        c=sqLiteDatabase.query(false,"person",strings,s,strings1,null,null,s1,null);        return c;    }    @Nullable    @Override    public String getType(Uri uri) {        return null;    }    @Nullable    @Override    public Uri insert(Uri uri, ContentValues contentValues) {        sqLiteDatabase.insert("person",null,contentValues);        return null;    }    @Override    public int delete(Uri uri, String s, String[] strings) {        sqLiteDatabase.delete("person",s,strings);        return 0;    }    @Override    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {        sqLiteDatabase.update("person",contentValues,s,strings);        return 0;    }}

然后在AndroidManifest.xml里的application里配置一个ContentProvider

<provider            android:authorities="com.zking.g150825_andorid16_sqlite.PERSONS"            android:name="com.zking.com.zking.provider.PersonsProvider"            android:exported="true"            ></provider>
android:authorities:uri
android:exported="true":设置此ContentProvider为可访问

然后创建一个ContentProvider的demo

界面main.xml

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="zking.com.myapplication.MainActivity">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入要查询的ID"        android:id="@+id/ed_main_id"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="查询"        android:onClick="query"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入要添加的姓名"        android:id="@+id/ed_main_name"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入要添加的年龄"        android:id="@+id/ed_main_age"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="添加"        android:onClick="insert"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入要删除的ID"        android:id="@+id/ed_main_id2"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="删除"        android:onClick="delete"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入要修改的ID"        android:id="@+id/ed_main_id3"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入修改后的姓名"        android:id="@+id/ed_main_name2"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入修改后的年龄"        android:id="@+id/ed_main_age2"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="修改"        android:onClick="update"        /></LinearLayout>

java类MainActivity

public class MainActivity extends AppCompatActivity {    private ContentResolver cr;    private EditText id;    private EditText name;    private EditText age;    private Uri uri;    private EditText age2;    private EditText name2;    private EditText id2;    private EditText id3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        cr = getContentResolver();        id = (EditText) findViewById(R.id.ed_main_id);        name = (EditText) findViewById(R.id.ed_main_name);        age = (EditText) findViewById(R.id.ed_main_age);        id2 = (EditText) findViewById(R.id.ed_main_id2);        id3 = (EditText) findViewById(R.id.ed_main_id3);        name2 = (EditText) findViewById(R.id.ed_main_name2);        age2 = (EditText) findViewById(R.id.ed_main_age2);        uri = Uri.parse("content://com.zking.g150825_andorid16_sqlite.PERSONS");    }    public void query(View view){        Cursor c= cr.query(uri,null,null,null,null);        while (c.moveToNext()){            int id=c.getInt(c.getColumnIndex("_id"));            String name=c.getString(c.getColumnIndex("name"));            int age=c.getInt(c.getColumnIndex("age"));            Log.i("test",id+" "+name+" "+age);        }    }    public void insert(View view){        ContentValues cv=new ContentValues();        cv.put("name",name.getText().toString());        cv.put("age",age.getText().toString());        cr.insert(uri,cv);    }    public void delete(View view){        cr.delete(uri,"_id=?",new String[]{id2.getText().toString()});    }    public void update(View view){        ContentValues cv=new ContentValues();        cv.put("name",name2.getText().toString());        cv.put("age",age2.getText().toString());        cr.update(uri,cv,"_id=?",new String[]{id3.getText().toString()});    }}这样我们就能够通过下面这个demo里的ContentResolver去调用上面那个demo里ContentProvider所提供的方法来操作Person表里的数据了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表