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 //ProvideTest18 /** Called when the activity is first created. */19 @Override20 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 Provider37 Cursor cur = managedQuery(uri_test, null, null, null, null);38 cur.moveToFirst();39 //ArrayList40 ArrayList
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 @Override22 public void onCreate(SQLiteDatabase db) {23 db.execSQL("CREATE TABLE test (" + BaseColumns._ID24 + " INTEGER PRIMARY KEY," + "name TEXT,"25 + "description TEXT" + ");");26 }27 28 @Override29 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {30 // TODO Auto-generated method stub31 db.execSQL("DROP TABLE IF EXISTS test");32 onCreate(db);33 }34 }35 //DatabaseHelper,databaseHelper36 DatabaseHelper databaseHelper;37 //Content Providers,onCreate()38 @Override39 public boolean onCreate() {40 databaseHelper = new DatabaseHelper(getContext());41 return true;42 }43 //Content Providers,insert()44 @Override45 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 @Override52 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 @Override63 public int delete(Uri uri, String selection, String[] selectionArgs) {64 // TODO Auto-generated method stub65 return 0;66 }67 //Content Providers,getType()68 @Override69 public String getType(Uri uri) {70 // TODO Auto-generated method stub71 return null;72 }73 //Content Providers,update()74 @Override75 public int update(Uri uri, ContentValues values, String selection,76 String[] selectionArgs) {77 // TODO Auto-generated method stub78 return 0;79 }80 }