Skip to content

Commit 5d1e6b9

Browse files
committed
Update task entry
1 parent a077693 commit 5d1e6b9

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

T09b.06-Exercise-UpdateTask/app/src/main/java/com/example/android/todolist/AddTaskActivity.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,29 @@ protected void onCreate(Bundle savedInstanceState) {
7171
mButton.setText(R.string.update_button);
7272
if (mTaskId == DEFAULT_TASK_ID) {
7373
// 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
7575
// Use DEFAULT_TASK_ID as the default
76+
mTaskId = intent.getIntExtra(EXTRA_TASK_ID, DEFAULT_TASK_ID);
7677

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
7879
// 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+
});
8597
}
8698
}
8799
}
@@ -114,9 +126,12 @@ public void onClick(View view) {
114126
* @param task the taskEntry to populate the UI
115127
*/
116128
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;
118131

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());
120135
}
121136

122137
/**
@@ -132,10 +147,15 @@ public void onSaveButtonClicked() {
132147
AppExecutors.getInstance().diskIO().execute(new Runnable() {
133148
@Override
134149
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
136151
// Otherwise update it
137152
// 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+
}
139159
finish();
140160
}
141161
});

T09b.06-Exercise-UpdateTask/app/src/main/java/com/example/android/todolist/MainActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public void run() {
141141
@Override
142142
public void onItemClickListener(int itemId) {
143143
// Launch AddTaskActivity adding the itemId as an extra in the intent
144-
// TODO (2) Launch AddTaskActivity with itemId as extra for the key AddTaskActivity.EXTRA_TASK_ID
144+
// Completed (2) Launch AddTaskActivity with itemId as extra for the key AddTaskActivity.EXTRA_TASK_ID
145+
Intent intent = new Intent(this, AddTaskActivity.class);
146+
intent.putExtra(AddTaskActivity.EXTRA_TASK_ID, itemId);
147+
startActivity(intent);
145148
}
146149
}

T09b.06-Exercise-UpdateTask/app/src/main/java/com/example/android/todolist/database/TaskDao.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public interface TaskDao {
2424
@Delete
2525
void deleteTask(TaskEntry taskEntry);
2626

27-
// TODO (1) Create a Query method named loadTaskById that receives an int id and returns a TaskEntry Object
27+
// Completed (1) Create a Query method named loadTaskById that receives an int id and returns a TaskEntry Object
2828
// The query for this method should get all the data for that id in the task table
29+
@Query("SELECT * FROM task WHERE id=:id")
30+
TaskEntry loadTaskById(int id);
2931
}

0 commit comments

Comments
 (0)