Skip to content

Commit 275c097

Browse files
committed
ListView loading is now done using AsyncTask with 0.5 sec delay
1 parent 9a133da commit 275c097

15 files changed

+152
-32
lines changed
805 Bytes
Binary file not shown.

TodoListManager/bin/classes.dex

2.86 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

TodoListManager/src/il/ac/huji/todolist/TodoListManagerActivity.java

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import il.ac.huji.todolist.ActionTaskDialog.DeleteTaskDialogListener;
44

5+
import java.util.ArrayList;
56
import java.util.Date;
67

78
//import com.parse.Parse;
89

10+
11+
12+
13+
914
import android.os.AsyncTask;
1015
import android.os.Build;
1116
import android.os.Bundle;
@@ -31,40 +36,80 @@
3136
*/
3237
public class TodoListManagerActivity extends Activity implements DeleteTaskDialogListener {
3338

34-
private TodoTaskCursorAdapter _adapter;
39+
// private TodoTaskCursorAdapter _adapter;
40+
private TodoTaskArrayAdapter _adapter;
3541
private TodoTaskSQLiteHelper _helper;
3642
private ActionTaskDialog _actionTaskDialog;
3743
private ListView _listToDoTask;
3844

39-
private class TodoTaskSaver extends AsyncTask<TodoTask, Void, Void> {
45+
private class TodoTaskSaver extends AsyncTask<TodoTask, Void, TodoTask[]> {
4046

4147
@Override
42-
protected Void doInBackground(TodoTask... tasks) {
48+
protected TodoTask[] doInBackground(TodoTask... tasks) {
4349
for(TodoTask task : tasks) {
44-
_helper.addTask(task);
50+
Long id = _helper.addTask(task);
51+
task.setID(id);
4552
}
46-
return null;
53+
return tasks;
54+
}
55+
56+
@Override
57+
protected void onPostExecute(TodoTask[] tasks) {
58+
for (TodoTask task : tasks)
59+
_adapter.add(task);
60+
_adapter.notifyDataSetChanged();
61+
}
62+
63+
}
64+
65+
private class TodoTaskDeleter extends AsyncTask<TodoTask, Void, TodoTask[]> {
66+
67+
@Override
68+
protected TodoTask[] doInBackground(TodoTask... tasks) {
69+
for (TodoTask task : tasks) {
70+
_helper.deleteTask(task.getID());
71+
}
72+
return tasks;
4773
}
4874

4975
@Override
50-
protected void onPostExecute(Void nothing) {
51-
_adapter.changeCursor(_helper.getAllTasks());
76+
protected void onPostExecute(TodoTask[] tasks) {
77+
for (TodoTask task : tasks)
78+
_adapter.remove(task);
79+
_adapter.notifyDataSetChanged();
5280
}
81+
5382
}
5483

55-
private class TodoTaskDeleter extends AsyncTask<Integer, Void, Void> {
84+
private class TodoTaskLoader extends AsyncTask<Cursor, TodoTask, Void> {
5685

5786
@Override
58-
protected Void doInBackground(Integer... positions) {
59-
for (Integer pos : positions) {
60-
_helper.deleteTask(pos);
87+
protected Void doInBackground(Cursor... cursors) {
88+
Cursor current = cursors[0];
89+
while (current.moveToNext()) {
90+
Long id = current.getLong(current.getColumnIndex(TodoTaskSQLiteHelper.COLUMN_ID));
91+
Long due = current.getLong(current.getColumnIndex(TodoTaskSQLiteHelper.COLUMN_DUE));
92+
String title = current.getString(current.getColumnIndex(TodoTaskSQLiteHelper.COLUMN_TITLE));
93+
94+
TodoTask task = new TodoTask(id, title, due);
95+
96+
try {
97+
Thread.sleep(500);
98+
} catch (InterruptedException e) {
99+
// well, shit
100+
}
101+
102+
publishProgress(task);
61103
}
62104
return null;
63105
}
64106

65107
@Override
66-
protected void onPostExecute(Void nothing) {
67-
_adapter.changeCursor(_helper.getAllTasks());
108+
protected void onProgressUpdate(TodoTask... tasks) {
109+
for (TodoTask task : tasks){
110+
_adapter.add(task);
111+
}
112+
_adapter.notifyDataSetChanged();
68113
}
69114

70115
}
@@ -81,13 +126,16 @@ protected void onCreate(Bundle savedInstanceState) {
81126

82127
_listToDoTask = (ListView) findViewById(R.id.list_todo_tasks);
83128

129+
_adapter = new TodoTaskArrayAdapter(getApplicationContext(),
130+
new ArrayList<TodoTask>());
131+
_listToDoTask.setAdapter(_adapter);
132+
84133
new Handler().post(new Runnable() {
85134

86135
@Override
87136
public void run() {
88-
_adapter = new TodoTaskCursorAdapter(TodoListManagerActivity.this,
89-
_helper.getAllTasks());
90-
_listToDoTask.setAdapter(_adapter);
137+
TodoTaskLoader loader = new TodoTaskLoader();
138+
loader.execute(_helper.getAllTasks());
91139
}
92140

93141
});
@@ -97,9 +145,8 @@ public void run() {
97145
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
98146
@SuppressLint("NewApi")
99147
public boolean onItemLongClick(AdapterView<?> parent, View child, int pos, long id) {
100-
Cursor current = (Cursor)_adapter.getItem(pos);
101-
_actionTaskDialog = new ActionTaskDialog(current.getInt(0),
102-
new TodoTask(current.getString(1)));
148+
TodoTask task = _adapter.getItem(pos);
149+
_actionTaskDialog = new ActionTaskDialog(pos, task);
103150
_actionTaskDialog.show(getFragmentManager(), "actionTask");
104151
return true;
105152
}
@@ -153,7 +200,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
153200
@Override
154201
public void onDialogPositiveClick(ActionTaskDialog dialog) {
155202
TodoTaskDeleter deleter = new TodoTaskDeleter();
156-
deleter.execute(dialog.getPos());
203+
TodoTask task = _adapter.getItem(dialog.getPos());
204+
deleter.execute(task);
157205
}
158206

159207
@Override

TodoListManager/src/il/ac/huji/todolist/TodoTask.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
public class TodoTask {
44

5-
private int _id;
5+
private Long _id;
66
private String _title;
77
private long _due;
88

99
public TodoTask() {
1010
}
1111

12-
public TodoTask(int id, String title, long due) {
12+
public TodoTask(Long id, String title, long due) {
1313
this._id = id;
1414
this._title = title;
1515
this._due = due;
@@ -24,11 +24,11 @@ public TodoTask(String title) {
2424
this._title = title;
2525
}
2626

27-
public int getID() {
27+
public Long getID() {
2828
return _id;
2929
}
3030

31-
public void setID(int id) {
31+
public void setID(Long id) {
3232
this._id = id;
3333
}
3434

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package il.ac.huji.todolist;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
6+
import android.content.Context;
7+
import android.graphics.Color;
8+
import android.text.format.DateFormat;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.ArrayAdapter;
13+
import android.widget.TextView;
14+
15+
/**
16+
* A custom array adapter for presenting the list with alternating color text.
17+
*
18+
* @author mickey
19+
*/
20+
public class TodoTaskArrayAdapter extends ArrayAdapter<TodoTask>{
21+
22+
private ArrayList<TodoTask> _listItems;
23+
private int _layout;
24+
25+
public TodoTaskArrayAdapter(Context context, ArrayList<TodoTask> listItems) {
26+
super(context, R.layout.todo_task, listItems);
27+
_layout = R.layout.todo_task;
28+
_listItems = listItems;
29+
}
30+
31+
/**
32+
* Display rows in alternating colors
33+
*/
34+
@Override
35+
public View getView(int position, View convertView, ViewGroup parent) {
36+
LayoutInflater inflater = LayoutInflater.from(getContext());
37+
38+
if (convertView == null) { // new row
39+
convertView = inflater.inflate(_layout, parent, false);
40+
}
41+
42+
TextView taskTitle = (TextView)convertView.findViewById(R.id.txtTodoTitle);
43+
taskTitle.setText(_listItems.get(position).getTitle());
44+
45+
TextView taskDueDate = (TextView)convertView.findViewById(R.id.txtTodoDueDate);
46+
47+
Long today = System.currentTimeMillis(), taskDue = _listItems.get(position).getDue();
48+
49+
if (taskDue == null) { // should not happen
50+
taskDueDate.setText(getContext().getResources().getString(R.string.no_due_date));
51+
}
52+
else {
53+
Date date = new Date();
54+
date.setTime(taskDue);
55+
String dueDate = DateFormat.getDateFormat(getContext()).format(date);
56+
taskDueDate.setText(dueDate);
57+
}
58+
59+
60+
if (today - taskDue > 0) { // over due
61+
taskTitle.setTextColor(Color.RED);
62+
taskDueDate.setTextColor(Color.RED);
63+
}
64+
else {
65+
taskTitle.setTextColor(Color.BLACK);
66+
taskDueDate.setTextColor(Color.BLACK);
67+
}
68+
69+
return convertView;
70+
}
71+
}

TodoListManager/src/il/ac/huji/todolist/TodoTaskSQLiteHelper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class TodoTaskSQLiteHelper extends SQLiteOpenHelper {
2020
private static TodoTaskSQLiteHelper _instanse;
2121

2222
private static final String TABLE_TASKS = "todo_tasks";
23-
private static final String COLUMN_ID = "_id";
24-
private static final String COLUMN_TITLE = "title";
25-
private static final String COLUMN_DUE = "due";
23+
static final String COLUMN_ID = "_id";
24+
static final String COLUMN_TITLE = "title";
25+
static final String COLUMN_DUE = "due";
2626

2727
private static final String DATABASE_NAME = "todo_db";
2828
private static final int DATABASE_VERSION = 1;
@@ -94,15 +94,15 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
9494
* A a new task to the database.
9595
* @param task The task to add.
9696
*/
97-
public void addTask(TodoTask task) {
97+
public Long addTask(TodoTask task) {
9898
// inserting row to local database
9999
SQLiteDatabase db = this.getWritableDatabase();
100100

101101
final ContentValues values = new ContentValues();
102102
values.put(COLUMN_TITLE, task.getTitle()); // task title
103103
values.put(COLUMN_DUE, task.getDue()); // task due
104104

105-
db.insert(TABLE_TASKS, null, values);
105+
Long id = db.insert(TABLE_TASKS, null, values);
106106
db.close(); // closing database connection
107107

108108
// insert row to Parse database
@@ -128,14 +128,15 @@ public void addTask(TodoTask task) {
128128

129129
Log.i(TodoTaskSQLiteHelper.class.getName(),
130130
"Inserting new task: " + task.getTitle() + " due: " + task.getDue());
131+
return id;
131132
}
132133

133134
/**
134135
* Get a task with an id.
135136
* @param id The id of the task as it appears in the database.
136137
* @return The TodoTask with the corresponding id.
137138
*/
138-
public TodoTask getTask(int id) {
139+
public TodoTask getTask(long id) {
139140
SQLiteDatabase db = this.getReadableDatabase();
140141

141142
Cursor cursor = db.query(
@@ -148,7 +149,7 @@ public TodoTask getTask(int id) {
148149
cursor.moveToFirst();
149150

150151
return new TodoTask(
151-
cursor.getInt(0), // id
152+
cursor.getLong(0), // id
152153
cursor.getString(1), // title
153154
cursor.getLong(2) // due date
154155
);
@@ -170,7 +171,7 @@ public Cursor getAllTasks() {
170171
* Delete a task with an id.
171172
* @param id The id of the task as it appears in the database.
172173
*/
173-
public void deleteTask(int id) {
174+
public void deleteTask(long id) {
174175
SQLiteDatabase db = this.getWritableDatabase();
175176

176177
db.delete(TABLE_TASKS, COLUMN_ID + " = ?",

0 commit comments

Comments
 (0)