您好,登錄后才能下訂單哦!
這篇文章給大家介紹ContentProvider怎么在Android應用中實現自定義,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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(""); }}); } }
關于ContentProvider怎么在Android應用中實現自定義就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。