Mm

1. n MVC ^ ' ac01 ma View fP Controller ^fe > AfHftÄMM^g View ^fe * DataProvider g Model ^fe * M SQLite Mg Model -

2. ac01 ^AA® Model(^ DataProvider)pM ' Model ftffi,$ - MM ' ac01

ftfgA =

private static final String[] PROJECTION = new String[] {

"stud_no", "stud_name", }; @Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); Intent intent = getIntent(); if (intent.getData() == null)

intent.setData(CONTENTURI); Cursor cur = getContentResolver().query(getIntent().getData(), PROJECTION, null, null, null);

3. AndroidManifest.xml

<provider android:name="DataProvider"

android:authorities="com.misoo.provider.SQ02"> </provider>

4. ^ SQLite m^ÄSÄ^mÄ^ ' adapter WM ' ListView M °

9.3 #23 : MM SQLite ® ContentProvider

AA-ffiMffi ' ffifflT ContentProvider ^ SQLite Ifif^I^i (Encapsulate)® ' flMfMAES ContentProvider ' M$PA

mrnmm query()®m ° |AT query()®mAA ' MA insert() * delete()A®m °

n^m^^m ContentProvider »A® ' MAMM SQLite »A®^ ?

° AA ContentProvider ^ATAM-MA® ' fMMfMA

n ' «AWM ContentProvider M^MMTOAM : Linter ®AA^f4

AH^ffiA SQLite 1ff4J¥AM ' MAA^ SQLite WM^mSMfg» Linter A« ' A Android °

2. ®T<Query>MM ' DispActivity ßfffi ' MTA SQLite «f4ÄÄ^ffi

DispActivity

S777, Lily Chang S888, Linda Lin S999, Bruce Wang ac01 ' DispActivity fM^^M ContentProvider ß query()®

SQ03

Query

Delete Exit

4. i£T<Delete> ' B®T<Query>MM ' RMM^i DispActivity ßf® ' MTA

OispActivity

Sill, Lily Chang S999, Bruce Wang

DlspActlvlty fP DataProvlder.java ° Step-2: MM ListActivity : acOl ' :

package com.misoo.SQ03; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.content. ContentValues; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget. SimpleAdapter;

public class acOl extends ListActivity {

public static final String AUTHORITY = "com.misoo.provider.SQ03";

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY +

private static final String[] PROJECTION = new String[] { "stud_no", // 0 "stud_name", // 1

@Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); Intent intent = getIntent(); if (intent.getData() == null)

{ intent. setData(CONTENT_ URI); } ArrayList<Map<String, Object» coll

= new ArrayList<Map<String, Object>>(); Map<String, Object item; item = new HashMap<String, Object>(); item.put("c1", " "Query"); coll.add(item); item = new HashMap<String, Object>(); item.put("c1", "Insert"); coll.add(item); item = new HashMap<String, Object>(); item.put("c1", "Delete"); coll.add(item); item = new HashMap<String, Object>(); item.put("c1", "Exit"); coll.add(item);

this.setListAdapter(new SimpleAdapter(this, coll, android.R.layouts/'mple_l/'st_item_1, new String[] { "c1" }, new int[] {android.R.id.text/}));

@Override protected void onListItemClick(ListView l, View v, int position, long id) { switch(position) { case 0: //Query

Intent intent = new Intent(); intent.setClass(ac01.this, DispActivity. class); startActivity(intent); break; case 1: //Insert

ContentValues cv = new ContentValues(); cv.put("stud_no", "S222"); cv.put("stud_name", "James Wu"); getContentResolver().insert(getIntent().getData(), cv); setTitle("Insert OK!"); break; case 2: // Delete

String cc [] = { "stud_no", "S888"};

getContentResolver(). delete(getIntent().getData(), "Student", cc);

setTitle("Delete OK!");

break;

Step-3: MM ListActivity : DispActivity > :

package com.misoo.SQ03; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget. SimpleAdapter; import android.widget.ListView;

public class DispActivity extends ListActivity {

public static final String AUTHORITY = "com.misoo.provider.SQ03";

public static final Uri CONTENT_ URI = Uriparse("content://" + AUTHORITY +

private static final String[] PROJECTION = new String[] { "stud_no", "stud_name" };

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.getData() == null)

intent.setData(CONTENT_ URI); Cursor cur = getContentResolver().query(getIntent().getData(), PROJECTION, null, null, null); ArrayList<Map<String, Object» coll

= new ArrayList<Map<String, Object>>(); Map<String, Object item; cur. moveToFirst(); while(!cur.isAfterLast()) {

item.put("c1", cur.getString(0) + ", " + cur.getString(1));

coll.add(item);

cur.moveToNext();

this.setListAdapter(new SimpleAdapter(this, coll, android.R. lay out. simple_list_item_1, new String[] { "c1" }, new int[] {android.R.id.fex/7}));

@Override protected void onListItemClick(ListView l, View v, int position, long id) { finish(); }

Step-4: MM Activity : DataProvider ' :

// — DataProvider.java fM^fif — package com.misoo.SQ03; import android.content.ContentProvider; import android.content. ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; import android.util.Log;

public class DataProvider extends ContentProvider {

private static final String DATABASE_NAME = "StudDB_22.db"; private static final int DATABASEVERSION = 2; private static final String TABLE_NAME = "Student";

private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE VERSION); } @Override public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " " + TABLE_NAME + " (" + "stud_no" + " TEXT," + "stud_name" + " TEXT" + ");");

String sql_1 = " "insert into " "+ TABLE_NAME +

" (stud_no, stud_name) values('S777', 'Lily Chang');"; String sql_2 = " "insert into " " + TABLENAME +

" (stud_no, stud_name) values('S888', 'Linda Lin');"; String sql_3 = " "insert into " " + TABLE_NAME +

" (stud_no, stud_name) values('S999', 'Bruce Wang');";

db.execSQL(sql_1); db.execSQL(sql_2); db.execSQL(sql_3);

@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}

private DatabaseHelper mOpenHelper; @Override public boolean onCreate() {

mOpenHelper = new DatabaseHelper(getContext()); return true; }

@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); Cursor c = db.query(TABLE_NAME, projection, null, null, null, null, null); return c; } @Override public String getType(Uri uri)

{ return null; } @Override public Uri insert(Uri uri, ContentValues initialValues) { String field_1 = initialValues.get("stud_no").toString(); String field_2 = initialValues.get("stud_name").toString(); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String sql_1 = "insert into "+ TABLE_NAME +

" (stud_no, stud_name) values('" + field_1 + "', '" + field_2 + "');"; try { db.execSQL(sql_1); }

catch (SQLException e) { Log.e("ERROR", e.toString()); } return uri; }

@Override public int delete(Uri uri, String where, String[] whereArgs) { SQLiteDatabase db = mOpenHelper.getWritableDatabase();

db.delete(where, whereArgs[0] + "='" + whereArgs[1] + , null);

@Override public int update(Uri uri, ContentValues values,

String where, String[] whereArgs) {

return 0;

Step-5: SQ02/AndroidManifest.xml ft^W ' :

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.misoo.SQ03">

<application android:icon="@drawable/icon" android:label="@string/app_name"> <provider android:name="DataProvider"

android:authorities="com.misoo.provider.SQ03"> </provider>

<activity android:name=".ac01" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

<activity android:name=".DispActivity" android:label="DispActivity"> </activity> </application> </manifest>

0 0

Post a comment