Skip to content

Commit

Permalink
SQLite functionality for the task list (using CursorAdapter)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickey946 committed Mar 29, 2014
1 parent eca0d8c commit 281b165
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 137 deletions.
2 changes: 1 addition & 1 deletion TodoListManager/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</intent-filter>
</activity>
<activity
android:name="il.ac.huji.todolist.AddNewTodoItemActivity"
android:name="il.ac.huji.todolist.AddNewTodoTaskActivity"
android:label="@string/title_activity_add_new_todo_item"
android:theme="@android:style/Theme.Holo.Light.Dialog" >
</activity>
Expand Down
2 changes: 1 addition & 1 deletion TodoListManager/bin/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</intent-filter>
</activity>
<activity
android:name="il.ac.huji.todolist.AddNewTodoItemActivity"
android:name="il.ac.huji.todolist.AddNewTodoTaskActivity"
android:label="@string/title_activity_add_new_todo_item"
android:theme="@android:style/Theme.Holo.Light.Dialog" >
</activity>
Expand Down
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file modified TodoListManager/bin/resources.ap_
Binary file not shown.
2 changes: 1 addition & 1 deletion TodoListManager/res/layout/activity_add_new_todo_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddNewTodoItemActivity" >
tools:context=".AddNewTodoTaskActivity" >

<EditText
android:id="@+id/edtNewItem"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @author mickey
*/
public class AddNewTodoItemActivity extends Activity {
public class AddNewTodoTaskActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
26 changes: 0 additions & 26 deletions TodoListManager/src/il/ac/huji/todolist/RetainedFragment.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

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

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

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.util.Pair;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
Expand All @@ -30,44 +28,38 @@
*/
public class TodoListManagerActivity extends Activity implements DeleteTaskDialogListener {

private RetainedFragment<ArrayList<Pair<String, Long>>> _dataFragment;
private ArrayAdapter<Pair<String, Long>> _adapter;
private ArrayList<Pair<String, Long>> _listItems;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);

// find the retained fragment on activity restarts
FragmentManager fm = getFragmentManager();
@SuppressWarnings("unchecked")
RetainedFragment<ArrayList<Pair<String, Long>>> findFragmentByTag =
(RetainedFragment<ArrayList<Pair<String, Long>>>) fm.findFragmentByTag("dataMain");
_dataFragment = findFragmentByTag;

// create the fragment and data the first time
if (_dataFragment == null) {
// add the fragment
_dataFragment = new RetainedFragment<ArrayList<Pair<String, Long>>>();
fm.beginTransaction().add(_dataFragment, "dataMain").commit();
// create new list
_dataFragment.setData(new ArrayList<Pair<String, Long>>());
}
_helper = new TodoTaskSQLiteHelper(this);

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

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

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

_listItems = _dataFragment.getData();
_adapter = new RowWithDateArrayAdapter(this, _listItems);
});

ListView listToDoTask = (ListView) findViewById(R.id.list_todo_tasks);
listToDoTask.setAdapter(_adapter);
listToDoTask.setOnItemLongClickListener(new OnItemLongClickListener() {
_listToDoTask.setOnItemLongClickListener(new OnItemLongClickListener() {

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public boolean onItemLongClick(AdapterView<?> parent, View child, int pos, long id) {
_actionTaskDialog = new ActionTaskDialog(pos, _listItems.get(pos).first);
Cursor current = (Cursor)_adapter.getItem(pos);
_actionTaskDialog = new ActionTaskDialog(current.getInt(0), current.getString(1));
_actionTaskDialog.show(getFragmentManager(), "actionTask");
return true;
}
Expand Down Expand Up @@ -96,7 +88,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
private static final int TASK_N_DATE = 15;

private void addTask() {
Intent intent = new Intent(this, AddNewTodoItemActivity.class);
Intent intent = new Intent(this, AddNewTodoTaskActivity.class);
startActivityForResult(intent, TASK_N_DATE);
}

Expand All @@ -111,17 +103,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// add the item
if(task != null) {
if(task.length() > 0) {
_listItems.add(new Pair<String, Long>(task, date));
_adapter.notifyDataSetChanged();
_helper.addTask(new TodoTask(task, date));
_adapter.changeCursor(_helper.getAllTasks());
}
}
}
}

@Override
public void onDialogPositiveClick(ActionTaskDialog dialog) {
_listItems.remove(dialog.getPos());
_adapter.notifyDataSetChanged();
_helper.deleteTask(dialog.getPos());
_adapter.changeCursor(_helper.getAllTasks());
}

@Override
Expand All @@ -132,6 +124,5 @@ public void onDialogNegativeClick(ActionTaskDialog dialog) {
@Override
public void onDestroy() {
super.onDestroy();
_dataFragment.setData(_listItems);
}
}
5 changes: 5 additions & 0 deletions TodoListManager/src/il/ac/huji/todolist/TodoTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public TodoTask(int id, String title, long due) {
this._title = title;
this._due = due;
}

public TodoTask(String title, long due) {
this._title = title;
this._due = due;
}

public int getID() {
return _id;
Expand Down
62 changes: 62 additions & 0 deletions TodoListManager/src/il/ac/huji/todolist/TodoTaskCursorAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package il.ac.huji.todolist;

import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TodoTaskCursorAdapter extends CursorAdapter {
private int _layout;

public TodoTaskCursorAdapter(Context context, Cursor c) {
super(context, c, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
_layout = R.layout.todo_task;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView taskTitle = (TextView)view.findViewById(R.id.txtTodoTitle);
taskTitle.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

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

Long today = System.currentTimeMillis();
Long taskDue = cursor.getLong(cursor.getColumnIndex(cursor.getColumnName(2)));

if (taskDue == null) { // should not happen
taskDueDate.setText(context.getResources().getString(R.string.no_due_date));
}
else {
Date date = new Date();
date.setTime(taskDue);
String dueDate = DateFormat.getDateFormat(context).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);
}
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time, we inflate the row layout
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(_layout, parent, false);

return view;
}

}
Loading

0 comments on commit 281b165

Please sign in to comment.