Add database helper

Add Task list activity
Add Create new task list
Add Custom CursorAdapter for task list view
This commit is contained in:
2015-11-25 18:03:55 -05:00
parent b5cc2ff82c
commit 31b9c8d3a3
12 changed files with 446 additions and 8 deletions

View File

@@ -20,8 +20,15 @@
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity" >
android:label="@string/settings_activity_title"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".TaskListActivity"
android:label="@string/task_list_activity_title"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />

View File

@@ -0,0 +1,58 @@
package com.wismna.geoffroy.donext;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by geoffroy on 15-11-25.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "donext.db";
public static final String COLUMN_ID = "_id";
public static final String TASKLIST_TABLE_NAME = "tasklist";
public static final String TASKLIST_COLUMN_NAME = "name";
private static final String TASKLIST_TABLE_CREATE =
"CREATE TABLE " + TASKLIST_TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TASKLIST_COLUMN_NAME + " TEXT NOT NULL);";
public static final String TASKS_TABLE_NAME = "tasks";
public static final String TASKS_COLUMN_NAME = "name";
public static final String TASKS_COLUMN_DESC = "description";
public static final String TASKS_COLUMN_CYCLE = "cycle";
public static final String TASKS_COLUMN_PRIORITY = "priority";
public static final String TASKS_COLUMN_DONE = "done";
public static final String TASKS_COLUMN_DELETED= "deleted";
public static final String TASKS_COLUMN_LIST = "list";
private static final String TASKS_TABLE_CREATE =
"CREATE TABLE " + TASKS_TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TASKS_COLUMN_NAME + " TEXT NOT NULL, " +
TASKS_COLUMN_DESC + " TEXT, " +
TASKS_COLUMN_CYCLE + " INTEGER NOT NULL, " +
TASKS_COLUMN_DONE + " INTEGER, " +
TASKS_COLUMN_DELETED + " INTEGER, " +
TASKS_COLUMN_PRIORITY + " INTEGER, " +
TASKS_COLUMN_LIST + " INTEGER NOT NULL, " +
"FOREIGN KEY(" + TASKS_COLUMN_LIST + ") REFERENCES " +
TASKLIST_TABLE_NAME + "(" + COLUMN_ID + ")" +
");";
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TASKLIST_TABLE_CREATE);
db.execSQL(TASKS_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

View File

@@ -69,6 +69,11 @@ public class MainActivity extends AppCompatActivity {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
/** Called when the user clicks the Edit Lists button */
public void openTaskLists(MenuItem menuItem) {
Intent intent = new Intent(this, TaskListActivity.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

View File

@@ -0,0 +1,76 @@
package com.wismna.geoffroy.donext;
/**
* Created by geoffroy on 15-11-25.
*/
public class Task {
private long id;
private String name;
private String description;
private int cycle;
private boolean deleted;
private long taskList;
private String taskListName;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String comment) {
this.name = comment;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCycle() {
return cycle;
}
public void setCycle(int cycle) {
this.cycle = cycle;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
public long getTaskList() {
return taskList;
}
public void setTaskList(long taskList) {
this.taskList = taskList;
}
public String getTaskListName() {
return taskListName;
}
public void setTaskListName(String taskListName) {
this.taskListName = taskListName;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,31 @@
package com.wismna.geoffroy.donext;
/**
* Created by geoffroy on 15-11-25.
*/
public class TaskList {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String comment) {
this.name = comment;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,74 @@
package com.wismna.geoffroy.donext;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
public class TaskListActivity extends ListActivity {
private TasksDataAccess dataAccess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_list);
ListView listView = (ListView) findViewById(android.R.id.list);
dataAccess = new TasksDataAccess(this);
dataAccess.open();
//List<TaskList> values = dataAccess.getAllTaskLists();
// use the SimpleCursorAdapter to show the
// elements in a ListView
/*ArrayAdapter<TaskList> adapter = new ArrayAdapter<TaskList>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);*/
TaskListCursorAdapter adapter = new TaskListCursorAdapter(
this, R.layout.item_task_list, dataAccess.getAllTaskListsCursor(), 0);
listView.setAdapter(adapter);
}
// Will be called when the create Task List button is clicked
public void onCreateTaskList(View view) {
@SuppressWarnings("unchecked")
//ArrayAdapter<TaskList> adapter = (ArrayAdapter<TaskList>) getListAdapter();
TaskListCursorAdapter adapter = (TaskListCursorAdapter) getListAdapter();
TaskList taskList;
EditText editText = (EditText) findViewById(R.id.new_task_list_name);
// save the new comment to the database
taskList = dataAccess.createTaskList(editText.getText().toString());
//adapter.add(taskList);
adapter.notifyDataSetChanged();
}
public void onDeleteTaskList(View view) {
@SuppressWarnings("unchecked")
//ArrayAdapter<TaskList> adapter = (ArrayAdapter<TaskList>) getListAdapter();
TaskListCursorAdapter adapter = (TaskListCursorAdapter) getListAdapter();
TaskList taskList;
if (adapter.getCount() > 0) {
taskList = (TaskList) getListAdapter().getItem(0);
dataAccess.deleteTaskList(taskList);
//adapter.remove(taskList);
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
dataAccess.open();
super.onResume();
}
@Override
protected void onPause() {
dataAccess.close();
super.onPause();
}
}

View File

@@ -0,0 +1,41 @@
package com.wismna.geoffroy.donext;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
/**
* Created by geoffroy on 15-11-25.
*/
public class TaskListCursorAdapter extends ResourceCursorAdapter {
public TaskListCursorAdapter(Context context, int layout, Cursor cursor, int flags) {
super(context, layout, cursor, flags);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_task_list, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView taskListCount = (TextView) view.findViewById(R.id.task_list_count);
TextView taskListName = (TextView) view.findViewById(R.id.task_list_name);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.TASKLIST_COLUMN_NAME));
// TODO: Update the count when tasks are implemented
int count = 0;
// Populate fields with extracted properties
taskListCount.setText(String.valueOf(count));
taskListName.setText(name);
}
}

View File

@@ -0,0 +1,81 @@
package com.wismna.geoffroy.donext;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by geoffroy on 15-11-25.
*/
public class TasksDataAccess {
// Database fields
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
private String[] taskListColumns = {DatabaseHelper.COLUMN_ID, DatabaseHelper.TASKLIST_COLUMN_NAME};
public TasksDataAccess(Context context) {
dbHelper = new DatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public TaskList createTaskList(String name) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.TASKLIST_COLUMN_NAME, name);
long insertId = database.insert(DatabaseHelper.TASKLIST_TABLE_NAME, null,
values);
Cursor cursor = database.query(DatabaseHelper.TASKLIST_TABLE_NAME,
taskListColumns, DatabaseHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
TaskList newTaskList = cursorToTaskList(cursor);
cursor.close();
return newTaskList;
}
public void deleteTaskList(TaskList comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(DatabaseHelper.TASKLIST_TABLE_NAME, DatabaseHelper.COLUMN_ID
+ " = " + id, null);
}
public List<TaskList> getAllTaskLists() {
List<TaskList> taskLists = new ArrayList<TaskList>();
Cursor cursor = getAllTaskListsCursor();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
TaskList taskList = cursorToTaskList(cursor);
taskLists.add(taskList);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return taskLists;
}
public Cursor getAllTaskListsCursor() {
return database.query(DatabaseHelper.TASKLIST_TABLE_NAME,
taskListColumns, null, null, null, null, null);
}
private TaskList cursorToTaskList(Cursor cursor) {
TaskList taskList = new TaskList();
taskList.setId(cursor.getLong(0));
taskList.setName(cursor.getString(1));
return taskList;
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.wismna.geoffroy.donext.TaskListActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/new_task_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/task_list_new_list_hint"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCreateTaskList"
android:text="@string/task_list_new_list_create"/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/task_list_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/task_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/task_list_delete"
android:onClick="onDeleteTaskList" />
</LinearLayout>

View File

@@ -8,14 +8,15 @@
android:title="@string/action_settings"
android:onClick="openSettings"
app:showAsAction="never" />
<item
android:id="@+id/action_newTab"
android:orderInCategory="50"
android:title="@string/action_newTab"
app:showAsAction="never" />
<item
android:id="@+id/action_editTabs"
android:orderInCategory="60"
android:title="@string/action_editTabs"
android:onClick="openTaskLists"
app:showAsAction="never" />
<item
android:id="@+id/action_about"
android:orderInCategory="50"
android:title="@string/action_about"
app:showAsAction="never" />
</menu>

View File

@@ -1,10 +1,18 @@
<resources>
<!-- Activities and menu strings -->
<string name="app_name">DoNExt</string>
<string name="action_settings">Settings</string>
<string name="action_newTab">New list</string>
<string name="action_editTabs">Edit lists</string>
<string name="action_about">About</string>
<string name="section_format">Hello World from section: %1$d</string>
<string name="title_activity_settings">Settings</string>
<string name="settings_activity_title">Settings</string>
<string name="task_list_activity_title">List edition</string>
<!-- Strings related to Task List edition -->
<string name="task_list_new_list_hint">New list name</string>
<string name="task_list_new_list_create">Create</string>
<string name="task_list_delete">Delete</string>
<!-- Strings related to Settings -->
<string name="settings_confirm_donext">Confirm on DoNext?</string>
@@ -20,4 +28,5 @@
<item>6</item>
<item>7</item>
</string-array>
<string name="title_activity_task_list">TaskListActivity</string>
</resources>