33import android .app .Activity ;
44import android .database .Cursor ;
55import android .os .Bundle ;
6+ import android .util .Log ;
67import android .view .View ;
78import android .widget .Button ;
89import android .widget .EditText ;
10+ import android .widget .Spinner ;
911import de .vogella .android .todos .database .TodoDbAdapter ;
1012
1113public class TodoEdit extends Activity {
1214 private EditText mTitleText ;
1315 private EditText mBodyText ;
1416 private Long mRowId ;
1517 private TodoDbAdapter mDbHelper ;
18+ private Spinner mCategory ;
1619
1720 @ Override
1821 protected void onCreate (Bundle bundle ) {
1922 super .onCreate (bundle );
20- mDbHelper = new TodoDbAdapter (this );
21- mDbHelper .open ();
23+ mDbHelper = new TodoDbAdapter (this );
24+ mDbHelper .open ();
2225 setContentView (R .layout .todo_edit );
26+ mCategory = (Spinner ) findViewById (R .id .category );
2327 mTitleText = (EditText ) findViewById (R .id .todo_edit_summary );
2428 mBodyText = (EditText ) findViewById (R .id .todo_edit_description );
29+
2530 Button confirmButton = (Button ) findViewById (R .id .todo_edit_button );
2631 mRowId = null ;
2732 Bundle extras = getIntent ().getExtras ();
@@ -44,6 +49,18 @@ private void populateFields() {
4449 if (mRowId != null ) {
4550 Cursor todo = mDbHelper .fetchTodo (mRowId );
4651 startManagingCursor (todo );
52+ String category = todo .getString (todo
53+ .getColumnIndexOrThrow (TodoDbAdapter .KEY_CATEGORY ));
54+
55+ for (int i =0 ; i <mCategory .getCount ();i ++){
56+
57+ String s = (String ) mCategory .getItemAtPosition (i );
58+ Log .e (null , s +" " + category );
59+ if (s .equalsIgnoreCase (category )){
60+ mCategory .setSelection (i );
61+ }
62+ }
63+
4764 mTitleText .setText (todo .getString (todo
4865 .getColumnIndexOrThrow (TodoDbAdapter .KEY_SUMMARY )));
4966 mBodyText .setText (todo .getString (todo
@@ -70,16 +87,18 @@ protected void onResume() {
7087 }
7188
7289 private void saveState () {
90+ String category = (String ) mCategory .getSelectedItem ();
7391 String summary = mTitleText .getText ().toString ();
7492 String description = mBodyText .getText ().toString ();
93+
7594
7695 if (mRowId == null ) {
77- long id = mDbHelper .createTodo (summary , description );
96+ long id = mDbHelper .createTodo (category , summary , description );
7897 if (id > 0 ) {
7998 mRowId = id ;
8099 }
81100 } else {
82- mDbHelper .updateTodo (mRowId , summary , description );
101+ mDbHelper .updateTodo (mRowId , category , summary , description );
83102 }
84103 }
85104}
0 commit comments