当前位置: 首页 > >

android 之内容提供者与访问者 --

发布时间:

写一个内容提供者的类,继承ContentProvider,配置内容提供者(authorities=“包名.想要命名的名字”name=“提供的类所在的包名.提供的类”).


android:authorities="com.example.mydber.person"
android:name="com.example.mydber.contentprovid.Provider"
android:exported="true">

测试提供者的数据



public class DbHelper extends SQLiteOpenHelper {

public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement,name,age)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}


public class MainActivity extends ListActivity {

private ListView listview;
private SQLiteDatabase datebase;
private EditText et_main_id;
private EditText et_main_name;
private EditText et_main_age;

@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 = getListView();
DbHelper dbHelper=new DbHelper(this,"pers.db",null,2);
datebase = dbHelper.getReadableDatabase();
Cursor cursor=datebase.query(false,"person",null,null,null,null,null,null,null);
SimpleCursorAdapter 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.setAdapter(simpleCursorAdapter);

}
public void save(View view){
String name=et_main_name.getText().toString();
String age=et_main_age.getText().toString();
//存到数据库


//HQL QBC 纯(原)
for (int i = 0; i <100 ; i++) {
ContentValues values=new ContentValues();//Map
values.put("name",name);
values.put("age",age);
values.putNull("_id");
datebase.insert("person","name",values);
// datebase.execSQL("insert into person values(null,?,?)",new String[]{name+i,age});
}
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}

}

新建一个Moudole获取内容访问者getContentResolver();用访问者调用查询.query();第一个参数域名uri,实例化uri,uri.parse(协议”content://”+authorities);写好后便能调用内容提供者的类中的query方法进行查询。返回cursor,然后进行循环取值。
接下来在query方法中写好查询就好了。详情请看代码。


用uri的匹配器查询单个:
实例化URI的匹配器UriMatcher(UriMatcher.NO_MATCH)。无匹配规则;
添加匹配规则第一个参域名 authorities,第三个规则的序列号。
uriMatcher.match(uri);获取序列
ContentUris.parseId(uri);获取uri后带的值#
以下为测试代码:



public class MainActivity extends AppCompatActivity {

private EditText et_main_id;
private ContentResolver cr;
private Uri uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cr = getContentResolver();
et_main_id = (EditText) findViewById(R.id.et_main_id);
}
public void getData(View view){
if (TextUtils.isEmpty(et_main_id.getText())){
//查询所有
uri = Uri.parse("content://com.example.mydber.person/person");
}else{
//查询单个
String id=et_main_id.getText().toString();
uri = Uri.parse("content://com.example.mydber.person/person/"+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("test",id+" "+name+" "+age);
}
}
}

可借看:http://blog.csdn.net/chenzheng_java/article/details/6232632



友情链接: