@@ -71,17 +71,29 @@ protected void onCreate(Bundle savedInstanceState) {
71
71
mButton .setText (R .string .update_button );
72
72
if (mTaskId == DEFAULT_TASK_ID ) {
73
73
// populate the UI
74
- // TODO (3) Assign the value of EXTRA_TASK_ID in the intent to mTaskId
74
+ // Completed (3) Assign the value of EXTRA_TASK_ID in the intent to mTaskId
75
75
// Use DEFAULT_TASK_ID as the default
76
+ mTaskId = intent .getIntExtra (EXTRA_TASK_ID , DEFAULT_TASK_ID );
76
77
77
- // TODO (4) Get the diskIO Executor from the instance of AppExecutors and
78
+ // Completed (4) Get the diskIO Executor from the instance of AppExecutors and
78
79
// call the diskIO execute method with a new Runnable and implement its run method
79
-
80
- // TODO (5) Use the loadTaskById method to retrieve the task with id mTaskId and
81
- // assign its value to a final TaskEntry variable
82
-
83
- // TODO (6) Call the populateUI method with the retrieve tasks
84
- // Remember to wrap it in a call to runOnUiThread
80
+ AppExecutors .getInstance ().diskIO ().execute (new Runnable () {
81
+ @ Override
82
+ public void run () {
83
+ // Completed (5) Use the loadTaskById method to retrieve the task with id mTaskId and
84
+ // assign its value to a final TaskEntry variable
85
+ final TaskEntry taskEntry = mDb .taskDao ().loadTaskById (mTaskId );
86
+
87
+ // Completed (6) Call the populateUI method with the retrieve tasks
88
+ // Remember to wrap it in a call to runOnUiThread
89
+ runOnUiThread (new Runnable () {
90
+ @ Override
91
+ public void run () {
92
+ populateUI (taskEntry );
93
+ }
94
+ });
95
+ }
96
+ });
85
97
}
86
98
}
87
99
}
@@ -114,9 +126,12 @@ public void onClick(View view) {
114
126
* @param task the taskEntry to populate the UI
115
127
*/
116
128
private void populateUI (TaskEntry task ) {
117
- // TODO (7) return if the task is null
129
+ // Completed (7) return if the task is null
130
+ if (task == null ) return ;
118
131
119
- // TODO (8) use the variable task to populate the UI
132
+ // Completed (8) use the variable task to populate the UI
133
+ mEditText .setText (task .getDescription ());
134
+ setPriorityInViews (task .getPriority ());
120
135
}
121
136
122
137
/**
@@ -132,10 +147,15 @@ public void onSaveButtonClicked() {
132
147
AppExecutors .getInstance ().diskIO ().execute (new Runnable () {
133
148
@ Override
134
149
public void run () {
135
- // TODO (9) insert the task only if mTaskId matches DEFAULT_TASK_ID
150
+ // Completed (9) insert the task only if mTaskId matches DEFAULT_TASK_ID
136
151
// Otherwise update it
137
152
// call finish in any case
138
- mDb .taskDao ().insertTask (taskEntry );
153
+ if (mTaskId == DEFAULT_TASK_ID ) {
154
+ mDb .taskDao ().insertTask (taskEntry );
155
+ } else {
156
+ taskEntry .setId (mTaskId );
157
+ mDb .taskDao ().updateTask (taskEntry );
158
+ }
139
159
finish ();
140
160
}
141
161
});
0 commit comments