Android 自定义ContentProvider简单实例
程序员文章站
2022-10-10 12:28:10
android 自定义contentprovider简单实例
android允许我们定义自己的的contentprovider对象来共享数据,练练手,简单来实现一下。...
android 自定义contentprovider简单实例
android允许我们定义自己的的contentprovider对象来共享数据,练练手,简单来实现一下。
要使用contentprovider来操作数据,必须要有保存数据的场所。可以使用文件或sqlite数据库的方式来保存数据,通常使用sqlite数据库。
1,创建一个数据库帮助类,归根结底都是它在操作数据库。代码如下:
package com.njue; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; public class dbmanager extends sqliteopenhelper { public static final string myprovider_table_name="myprovider_table_name"; string sql="create table myprovider_table_name(id integer,name text)"; public dbmanager(context context) { super(context, "myprivoder.db", null, 1); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase arg0) { // todo auto-generated method stub arg0.execsql(sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } }
2,创建一个myprovider类,该类继承contentprovider,实现其抽象方法来操作数据库,该类引用dbmanager类来获得数据库实例。
package com.njue; import android.content.contentprovider; import android.content.contentvalues; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.net.uri; public class myprovider extends contentprovider { dbmanager dbhelper; @override public int delete(uri arg0, string arg1, string[] arg2) { // todo auto-generated method stub sqlitedatabase db=dbhelper.getwritabledatabase(); int id=db.delete(dbmanager.myprovider_table_name, arg1, arg2); if(id!=0)return id; return 0; } @override public string gettype(uri uri) { // todo auto-generated method stub return null; } @override public uri insert(uri uri, contentvalues values) { // todo auto-generated method stub sqlitedatabase db=dbhelper.getwritabledatabase(); db.insert(dbmanager.myprovider_table_name, "id", values); return null; } @override public boolean oncreate() { // todo auto-generated method stub dbhelper=new dbmanager(getcontext()); return true; } @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { // todo auto-generated method stub sqlitedatabase db=dbhelper.getwritabledatabase(); cursor c=db.query(dbmanager.myprovider_table_name,projection, selection, selectionargs, null, null, sortorder); return c; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { // todo auto-generated method stub sqlitedatabase db=dbhelper.getwritabledatabase(); int id=db.update(dbmanager.myprovider_table_name, values, selection, selectionargs); if(id!=0)return id; return 0; } }
3,contextprovider必须声明才能使用,因此,需要在androidmanifest.xml配置文件中声明。声明很简单,需要类名称和授权名称两个属性。
<provider android:name="myprovider" android:authorities="com.njue.myprovider" />
到这里,我们自定义的contentprovider就完成了。我们可以在eclipse中的file explorer 窗口中看到自定义的contentprovider
4,简单测试
package com.njue; import android.app.activity; import android.content.contentresolver; import android.content.contentvalues; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class content_provideractivity extends activity { /** called when the activity is first created. */ uri uri; string authority="content://com.njue.myprovider/myprovider_table_name"; contentresolver cr; edittext input; button insert; textview show; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); input=(edittext)findviewbyid(r.id.input); insert=(button)findviewbyid(r.id.insert); show=(textview)findviewbyid(r.id.show); uri=uri.parse(authority); cr=this.getcontentresolver(); insert.setonclicklistener(new button.onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub contentvalues values=new contentvalues(); values.put("name", input.gettext().tostring()); cr.insert(uri, values); cursor c=cr.query(uri, null, null, null, null); show.settext(""); c.movetofirst(); while(c.movetonext()){ show.append(c.getstring(1)+"\r\n"); } input.settext(""); }}); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!