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(""); }}); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
赞 (0)