安卓初級教程(3):ContentProvider的運用原理
1 package com.example.android.provider; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Map; 6 import android.app.Activity; 7 import android.content.ContentValues; 8 import android.database.Cursor; 9 import android.net.Uri; 10 import android.os.Bundle; 11 import android.widget.ListView; 12 import android.widget.SimpleAdapter; 13 14 //這是第一個先看的java檔,testprovider.java是繼承這個java檔的。 15 16 public class ProviderTest extends Activity { 17 //ProvideTest 18 /** Called when the activity is first created. */ 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 //Content Provider和Uri。我們要學習的重中之重就在以下這兩行。 23 //在這裡getIntent()是意圖從另外一個java檔中獲取數據。如何從另一個java檔中抓取數據,就是借助Uri.parse。 24 //Uri uri_test是定義項,為了之後在Cusor cur之中抓取數據。 25 getIntent().setData(Uri.parse("content://com.example.android.provider.testprovider")); 26 Uri uri_test = getIntent().getData(); 27 28 //精妙之處就在於這個values和另一個java檔之中的values是處於何種關係?手 29 ContentValues values = new ContentValues(); 30 values.put("name", "陳翹鈞"); 31 values.put("description", "68206021"); 32 getContentResolver().insert(uri_test, values); 33 values.put("name", "馮幹傑"); 34 values.put("description", "60746017"); 35 getContentResolver().insert(uri_test, values); 36 //Content Provider 37 Cursor cur = managedQuery(uri_test, null, null, null, null); 38 cur.moveToFirst(); 39 //ArrayList 40 ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); 41 Map<String, Object> item; 42 //ArrayList data 43 for (int i = 0; i < cur.getCount(); i++) { 44 item = new HashMap<String, Object>(); 45 item.put("column00", cur.getString(0)); 46 item.put("column01", cur.getString(1)); 47 item.put("column02", cur.getString(2)); 48 data.add(item); 49 cur.moveToNext(); 50 } 51 cur.close(); 52 //ArrayList data,mListView01 53 ListView mListView01 = new ListView(this); 54 SimpleAdapter adapter = new SimpleAdapter(this, data, 55 R.layout.main, new String[] {"column00", "column01", "column02" }, new int[] { 56 R.id.TextView01, R.id.TextView02, R.id.TextView03 }); 57 mListView01.setAdapter(adapter); 58 setContentView(mListView01); 59 } 60 }
1 package com.example.android.provider; 2 3 import android.content.ContentProvider; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 import android.database.sqlite.SQLiteOpenHelper; 9 import android.database.sqlite.SQLiteQueryBuilder; 10 import android.net.Uri; 11 import android.provider.BaseColumns; 12 13 public class TestProvider extends ContentProvider { 14 15 private static class DatabaseHelper extends SQLiteOpenHelper { 16 17 DatabaseHelper(Context context) { 18 super(context, "test.db", null, 1); 19 } 20 21 @Override 22 public void onCreate(SQLiteDatabase db) { 23 db.execSQL("CREATE TABLE test (" + BaseColumns._ID 24 + " INTEGER PRIMARY KEY," + "name TEXT," 25 + "description TEXT" + ");"); 26 } 27 28 @Override 29 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 30 // TODO Auto-generated method stub 31 db.execSQL("DROP TABLE IF EXISTS test"); 32 onCreate(db); 33 } 34 } 35 //DatabaseHelper,databaseHelper 36 DatabaseHelper databaseHelper; 37 //Content Providers,onCreate() 38 @Override 39 public boolean onCreate() { 40 databaseHelper = new DatabaseHelper(getContext()); 41 return true; 42 } 43 //Content Providers,insert() 44 @Override 45 public Uri insert(Uri uri, ContentValues values) { 46 SQLiteDatabase db = databaseHelper.getWritableDatabase(); 47 db.insert("test", null, values); 48 return null; 49 } 50 //Content Providers,query() 51 @Override 52 public Cursor query(Uri uri, String[] projection, String selection, 53 String[] selectionArgs, String sortOrder) { 54 SQLiteDatabase db = databaseHelper.getReadableDatabase(); 55 SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 56 qb.setTables("test"); 57 Cursor c = qb.query(db, projection, selection, selectionArgs, null, 58 null, null); 59 return c; 60 } 61 //Content Providers,delete() 62 @Override 63 public int delete(Uri uri, String selection, String[] selectionArgs) { 64 // TODO Auto-generated method stub 65 return 0; 66 } 67 //Content Providers,getType() 68 @Override 69 public String getType(Uri uri) { 70 // TODO Auto-generated method stub 71 return null; 72 } 73 //Content Providers,update() 74 @Override 75 public int update(Uri uri, ContentValues values, String selection, 76 String[] selectionArgs) { 77 // TODO Auto-generated method stub 78 return 0; 79 } 80 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。