Skip to content

Commit

Permalink
ListView loading is now done using AsyncTask with 0.5 sec delay
Browse files Browse the repository at this point in the history
  • Loading branch information
mickey946 committed Apr 28, 2014
1 parent 9a133da commit 275c097
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 32 deletions.
Binary file modified TodoListManager/bin/TodoListManager.apk
Binary file not shown.
Binary file modified TodoListManager/bin/classes.dex
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.
Binary file modified TodoListManager/bin/classes/il/ac/huji/todolist/TodoTask.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

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

import java.util.ArrayList;
import java.util.Date;

//import com.parse.Parse;





import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -31,40 +36,80 @@
*/
public class TodoListManagerActivity extends Activity implements DeleteTaskDialogListener {

private TodoTaskCursorAdapter _adapter;
// private TodoTaskCursorAdapter _adapter;
private TodoTaskArrayAdapter _adapter;
private TodoTaskSQLiteHelper _helper;
private ActionTaskDialog _actionTaskDialog;
private ListView _listToDoTask;

private class TodoTaskSaver extends AsyncTask<TodoTask, Void, Void> {
private class TodoTaskSaver extends AsyncTask<TodoTask, Void, TodoTask[]> {

@Override
protected Void doInBackground(TodoTask... tasks) {
protected TodoTask[] doInBackground(TodoTask... tasks) {
for(TodoTask task : tasks) {
_helper.addTask(task);
Long id = _helper.addTask(task);
task.setID(id);
}
return null;
return tasks;
}

@Override
protected void onPostExecute(TodoTask[] tasks) {
for (TodoTask task : tasks)
_adapter.add(task);
_adapter.notifyDataSetChanged();
}

}

private class TodoTaskDeleter extends AsyncTask<TodoTask, Void, TodoTask[]> {

@Override
protected TodoTask[] doInBackground(TodoTask... tasks) {
for (TodoTask task : tasks) {
_helper.deleteTask(task.getID());
}
return tasks;
}

@Override
protected void onPostExecute(Void nothing) {
_adapter.changeCursor(_helper.getAllTasks());
protected void onPostExecute(TodoTask[] tasks) {
for (TodoTask task : tasks)
_adapter.remove(task);
_adapter.notifyDataSetChanged();
}

}

private class TodoTaskDeleter extends AsyncTask<Integer, Void, Void> {
private class TodoTaskLoader extends AsyncTask<Cursor, TodoTask, Void> {

@Override
protected Void doInBackground(Integer... positions) {
for (Integer pos : positions) {
_helper.deleteTask(pos);
protected Void doInBackground(Cursor... cursors) {
Cursor current = cursors[0];
while (current.moveToNext()) {
Long id = current.getLong(current.getColumnIndex(TodoTaskSQLiteHelper.COLUMN_ID));
Long due = current.getLong(current.getColumnIndex(TodoTaskSQLiteHelper.COLUMN_DUE));
String title = current.getString(current.getColumnIndex(TodoTaskSQLiteHelper.COLUMN_TITLE));

TodoTask task = new TodoTask(id, title, due);

try {
Thread.sleep(500);
} catch (InterruptedException e) {
// well, shit
}

publishProgress(task);
}
return null;
}

@Override
protected void onPostExecute(Void nothing) {
_adapter.changeCursor(_helper.getAllTasks());
protected void onProgressUpdate(TodoTask... tasks) {
for (TodoTask task : tasks){
_adapter.add(task);
}
_adapter.notifyDataSetChanged();
}

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

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

_adapter = new TodoTaskArrayAdapter(getApplicationContext(),
new ArrayList<TodoTask>());
_listToDoTask.setAdapter(_adapter);

new Handler().post(new Runnable() {

@Override
public void run() {
_adapter = new TodoTaskCursorAdapter(TodoListManagerActivity.this,
_helper.getAllTasks());
_listToDoTask.setAdapter(_adapter);
TodoTaskLoader loader = new TodoTaskLoader();
loader.execute(_helper.getAllTasks());
}

});
Expand All @@ -97,9 +145,8 @@ public void run() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public boolean onItemLongClick(AdapterView<?> parent, View child, int pos, long id) {
Cursor current = (Cursor)_adapter.getItem(pos);
_actionTaskDialog = new ActionTaskDialog(current.getInt(0),
new TodoTask(current.getString(1)));
TodoTask task = _adapter.getItem(pos);
_actionTaskDialog = new ActionTaskDialog(pos, task);
_actionTaskDialog.show(getFragmentManager(), "actionTask");
return true;
}
Expand Down Expand Up @@ -153,7 +200,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override
public void onDialogPositiveClick(ActionTaskDialog dialog) {
TodoTaskDeleter deleter = new TodoTaskDeleter();
deleter.execute(dialog.getPos());
TodoTask task = _adapter.getItem(dialog.getPos());
deleter.execute(task);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions TodoListManager/src/il/ac/huji/todolist/TodoTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

public class TodoTask {

private int _id;
private Long _id;
private String _title;
private long _due;

public TodoTask() {
}

public TodoTask(int id, String title, long due) {
public TodoTask(Long id, String title, long due) {
this._id = id;
this._title = title;
this._due = due;
Expand All @@ -24,11 +24,11 @@ public TodoTask(String title) {
this._title = title;
}

public int getID() {
public Long getID() {
return _id;
}

public void setID(int id) {
public void setID(Long id) {
this._id = id;
}

Expand Down
71 changes: 71 additions & 0 deletions TodoListManager/src/il/ac/huji/todolist/TodoTaskArrayAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package il.ac.huji.todolist;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
* A custom array adapter for presenting the list with alternating color text.
*
* @author mickey
*/
public class TodoTaskArrayAdapter extends ArrayAdapter<TodoTask>{

private ArrayList<TodoTask> _listItems;
private int _layout;

public TodoTaskArrayAdapter(Context context, ArrayList<TodoTask> listItems) {
super(context, R.layout.todo_task, listItems);
_layout = R.layout.todo_task;
_listItems = listItems;
}

/**
* Display rows in alternating colors
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());

if (convertView == null) { // new row
convertView = inflater.inflate(_layout, parent, false);
}

TextView taskTitle = (TextView)convertView.findViewById(R.id.txtTodoTitle);
taskTitle.setText(_listItems.get(position).getTitle());

TextView taskDueDate = (TextView)convertView.findViewById(R.id.txtTodoDueDate);

Long today = System.currentTimeMillis(), taskDue = _listItems.get(position).getDue();

if (taskDue == null) { // should not happen
taskDueDate.setText(getContext().getResources().getString(R.string.no_due_date));
}
else {
Date date = new Date();
date.setTime(taskDue);
String dueDate = DateFormat.getDateFormat(getContext()).format(date);
taskDueDate.setText(dueDate);
}


if (today - taskDue > 0) { // over due
taskTitle.setTextColor(Color.RED);
taskDueDate.setTextColor(Color.RED);
}
else {
taskTitle.setTextColor(Color.BLACK);
taskDueDate.setTextColor(Color.BLACK);
}

return convertView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class TodoTaskSQLiteHelper extends SQLiteOpenHelper {
private static TodoTaskSQLiteHelper _instanse;

private static final String TABLE_TASKS = "todo_tasks";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DUE = "due";
static final String COLUMN_ID = "_id";
static final String COLUMN_TITLE = "title";
static final String COLUMN_DUE = "due";

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

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

db.insert(TABLE_TASKS, null, values);
Long id = db.insert(TABLE_TASKS, null, values);
db.close(); // closing database connection

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

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

/**
* Get a task with an id.
* @param id The id of the task as it appears in the database.
* @return The TodoTask with the corresponding id.
*/
public TodoTask getTask(int id) {
public TodoTask getTask(long id) {
SQLiteDatabase db = this.getReadableDatabase();

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

return new TodoTask(
cursor.getInt(0), // id
cursor.getLong(0), // id
cursor.getString(1), // title
cursor.getLong(2) // due date
);
Expand All @@ -170,7 +171,7 @@ public Cursor getAllTasks() {
* Delete a task with an id.
* @param id The id of the task as it appears in the database.
*/
public void deleteTask(int id) {
public void deleteTask(long id) {
SQLiteDatabase db = this.getWritableDatabase();

db.delete(TABLE_TASKS, COLUMN_ID + " = ?",
Expand Down

0 comments on commit 275c097

Please sign in to comment.