22
33import android .app .Activity ;
44import android .content .Intent ;
5+ import android .database .Cursor ;
56import android .os .Bundle ;
67import android .view .View ;
78import android .widget .Button ;
@@ -12,45 +13,86 @@ public class TodoEdit extends Activity {
1213 private EditText mTitleText ;
1314 private EditText mBodyText ;
1415 private Long mRowId ;
16+ private TodoDbAdapter mDbHelper ;
1517
1618 @ Override
1719 protected void onCreate (Bundle bundle ) {
1820 super .onCreate (bundle );
19-
21+ mDbHelper = new TodoDbAdapter (this );
22+ mDbHelper .open ();
2023 setContentView (R .layout .todo_edit );
2124 mTitleText = (EditText ) findViewById (R .id .todo_edit_summary );
2225 mBodyText = (EditText ) findViewById (R .id .todo_edit_description );
2326 Button confirmButton = (Button ) findViewById (R .id .todo_edit_button );
2427 mRowId = null ;
2528 Bundle extras = getIntent ().getExtras ();
29+ mRowId = (bundle == null ) ? null : (Long ) bundle
30+ .getSerializable (TodoDbAdapter .KEY_ROWID );
2631 if (extras != null ) {
27- String title = extras .getString (TodoDbAdapter .KEY_SUMMARY );
28- String body = extras .getString (TodoDbAdapter .KEY_DESCRIPTION );
29- mRowId = extras .getLong (TodoDbAdapter .KEY_ROWID );
30-
31- if (title != null ) {
32- mTitleText .setText (title );
33- }
34- if (body != null ) {
35- mBodyText .setText (body );
36- }
32+ mRowId = extras .getLong (TodoDbAdapter .KEY_ROWID );
3733 }
34+ System .out .println ("So far so good" );
35+ populateFields ();
3836 confirmButton .setOnClickListener (new View .OnClickListener () {
39-
40- public void onClick (View view ) {
41- Bundle bundle = new Bundle ();
42- bundle .putString (TodoDbAdapter .KEY_SUMMARY , mTitleText .getText ().toString ());
43- bundle .putString (TodoDbAdapter .KEY_DESCRIPTION , mBodyText .getText ().toString ());
44- if (mRowId != null ) {
45- bundle .putLong (TodoDbAdapter .KEY_ROWID , mRowId );
46- }
47-
48- Intent mIntent = new Intent ();
49- mIntent .putExtras (bundle );
50- setResult (RESULT_OK , mIntent );
51- finish ();
52- }
53-
37+ public void onClick (View view ) {
38+ Bundle bundle = new Bundle ();
39+ bundle .putString (TodoDbAdapter .KEY_SUMMARY , mTitleText
40+ .getText ().toString ());
41+ bundle .putString (TodoDbAdapter .KEY_DESCRIPTION , mBodyText
42+ .getText ().toString ());
43+ if (mRowId != null ) {
44+ bundle .putLong (TodoDbAdapter .KEY_ROWID , mRowId );
45+ }
46+
47+ Intent mIntent = new Intent ();
48+ mIntent .putExtras (bundle );
49+ setResult (RESULT_OK , mIntent );
50+ finish ();
51+ }
52+
5453 });
5554 }
55+
56+ private void populateFields () {
57+ if (mRowId != null ) {
58+ Cursor todo = mDbHelper .fetchTodo (mRowId );
59+ startManagingCursor (todo );
60+ mTitleText .setText (todo .getString (todo
61+ .getColumnIndexOrThrow (TodoDbAdapter .KEY_SUMMARY )));
62+ mBodyText .setText (todo .getString (todo
63+ .getColumnIndexOrThrow (TodoDbAdapter .KEY_DESCRIPTION )));
64+ }
65+ }
66+
67+ protected void onSaveInstanceState (Bundle outState ) {
68+ super .onSaveInstanceState (outState );
69+ saveState ();
70+ outState .putSerializable (TodoDbAdapter .KEY_ROWID , mRowId );
71+ }
72+
73+ @ Override
74+ protected void onPause () {
75+ super .onPause ();
76+ saveState ();
77+ }
78+
79+ @ Override
80+ protected void onResume () {
81+ super .onResume ();
82+ populateFields ();
83+ }
84+
85+ private void saveState () {
86+ String summary = mTitleText .getText ().toString ();
87+ String description = mBodyText .getText ().toString ();
88+
89+ if (mRowId == null ) {
90+ long id = mDbHelper .createTodo (summary , description );
91+ if (id > 0 ) {
92+ mRowId = id ;
93+ }
94+ } else {
95+ mDbHelper .updateTodo (mRowId , summary , description );
96+ }
97+ }
5698}
0 commit comments