NotePad Tutorial
の v3 なソレを ContentProvider 使う形でモディファイしてみました。実は ContentProvider なレクチャ資料を作らねばならない事情がありまして (ry
お上品なツクリ
実は以前、Notepad Tutorial なハッカソンもどきをした事がありまして、最終版は NotesDBAdapter クラスが DB とのやりとりを抽象化してて、SQLite が例えば GAE なソレになったとしても、修正はこのクラスに限定できるはず、的な話をした事がありまして、ContentProvider に書き換えるのもできるはず、というのが今回の修正のポイントとなります。
とりあえず微妙な部分を省いた形で実装を以下に。
package com.android.demo.notepad3; 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 NotesDbAdapter { public static final Uri CONTENT_URI = Uri.parse("content://com.android.demo.notepad3/note"); public static final String KEY_TITLE = "title"; public static final String KEY_BODY = "body"; public static final String KEY_ROWID = "_id"; private static final String TAG = "NotesDbAdapter"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; /** * Database creation sql statement */ private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, " + "title text not null, body text not null);"; private static final String DATABASE_NAME = "data"; private static final String DATABASE_TABLE = "notes"; private static final int DATABASE_VERSION = 2; private final Context mCtx; public NotesDbAdapter(Context ctx) { this.mCtx = ctx; } public long createNote(String title, String body) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_TITLE, title); initialValues.put(KEY_BODY, body); Uri tmp = mCtx.getContentResolver().insert(CONTENT_URI, initialValues); return Long.parseLong(tmp.getLastPathSegment()); } public boolean deleteNote(long rowId) { return mCtx.getContentResolver().delete(CONTENT_URI, null, null) > 0; } public Cursor fetchAllNotes() { return mCtx.getContentResolver().query(CONTENT_URI, null, null, null, "_id DESC"); } public Cursor fetchNote(long rowId) throws SQLException { Cursor mCursor = mCtx.getContentResolver().query(CONTENT_URI, null, "_id = " + rowId, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } public boolean updateNote(long rowId, String title, String body) { ContentValues args = new ContentValues(); args.put(KEY_TITLE, title); args.put(KEY_BODY, body); return mCtx.getContentResolver().update(CONTENT_URI, args, "_id = " + rowId, null) > 0; } }
楽ちんだなぁ、と言いつつ動作確認してみたらオチる。
Unable to stop activity との事。ぬるぽ云々との事なんですが、オチてるのが Activity の中で全然意味が分かりませんでした。onStop で云々とか onPause で云々とか意味分からんかったのですが、ココで cursor を release しとるか? 的な指摘が入ってるみたいだったので Activity 側で Cursor 使ってる箇所について、close メソドを呼び出す処理を追加したらオチなくなりました。
例えば NoteEdit クラスの以下とか
private void populateFields() { if (mRowId != null) { Cursor note = mDbHelper.fetchNote(mRowId); startManagingCursor(note); mTitleText.setText(note.getString( note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); mBodyText.setText(note.getString( note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))); note.close(); } }
NotePad クラスで onPause/onResume を追加するとか。
@Override protected void onPause() { super.onPause(); mNotesCursor.close(); } @Override protected void onResume() { super.onResume(); fillData(); }
時間が取れたら再現試験とかしてみたいですがどうなるやら。