Task list displays cycle count and task count

This commit is contained in:
2015-11-29 18:09:55 -05:00
parent 354618ccab
commit 9cd48959b1
8 changed files with 101 additions and 37 deletions

View File

@@ -14,6 +14,7 @@ import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.RadioButton; import android.widget.RadioButton;
import android.widget.RadioGroup; import android.widget.RadioGroup;
@@ -175,6 +176,12 @@ public class MainActivity extends AppCompatActivity implements NewTaskFragment.N
newTaskFragment.show(manager, "Create new task"); newTaskFragment.show(manager, "Create new task");
} }
/** Will be called when the delete Task button is clicked */
public void onDeleteTask(View view) {
RecyclerView recyclerView = (RecyclerView) view;
}
@Override @Override
public void onListFragmentInteraction(Task item) { public void onListFragmentInteraction(Task item) {

View File

@@ -37,7 +37,7 @@ public class TaskListActivity extends AppCompatActivity {
updateCreateButtonEnabled(); updateCreateButtonEnabled();
} }
// Will be called when the create Task List button is clicked /** Will be called when the create Task List button is clicked */
public void onCreateTaskList(View view) { public void onCreateTaskList(View view) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
EditText editText = (EditText) findViewById(R.id.new_task_list_name); EditText editText = (EditText) findViewById(R.id.new_task_list_name);
@@ -48,6 +48,7 @@ public class TaskListActivity extends AppCompatActivity {
updateCreateButtonEnabled(); updateCreateButtonEnabled();
} }
/** Will be called when the delete Task List button is clicked */
public void onDeleteTaskList(View view) { public void onDeleteTaskList(View view) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final int position = listView.getPositionForView((View) view.getParent()); final int position = listView.getPositionForView((View) view.getParent());

View File

@@ -16,7 +16,7 @@ import java.util.List;
* {@link RecyclerView.Adapter} that can display a {@link Task} and makes a call to the * {@link RecyclerView.Adapter} that can display a {@link Task} and makes a call to the
* specified {@link OnListFragmentInteractionListener}. * specified {@link OnListFragmentInteractionListener}.
*/ */
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> { public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> implements View.OnClickListener {
private final List<Task> mValues; private final List<Task> mValues;
private final OnListFragmentInteractionListener mListener; private final OnListFragmentInteractionListener mListener;
@@ -30,14 +30,17 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()) View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_task, parent, false); .inflate(R.layout.fragment_task, parent, false);
return new ViewHolder(view);
ViewHolder holder = new ViewHolder(view);
holder.mTitleView.setOnClickListener(TaskAdapter.this);
return holder;
} }
@Override @Override
public void onBindViewHolder(final ViewHolder holder, int position) { public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position); holder.mItem = mValues.get(position);
holder.mIdView.setText(String.valueOf(holder.mItem.getCycle())); holder.mCycleView.setText(String.valueOf(holder.mItem.getCycle()));
holder.mContentView.setText(holder.mItem.getName()); holder.mTitleView.setText(holder.mItem.getName());
holder.mView.setOnClickListener(new View.OnClickListener() { holder.mView.setOnClickListener(new View.OnClickListener() {
@Override @Override
@@ -56,6 +59,13 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
return mValues.size(); return mValues.size();
} }
@Override
public void onClick(View view) {
/*ViewHolder holder = (ViewHolder) view.getTag();
if (view.getId() == holder.mTitleView.getId()) {
Toast.makeText(view.getContext(), holder.mTitleView.getText(), Toast.LENGTH_SHORT).show();
}*/
}
public void add(Task item, int position) { public void add(Task item, int position) {
mValues.add(position, item); mValues.add(position, item);
notifyItemInserted(position); notifyItemInserted(position);
@@ -69,20 +79,20 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder { public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView; public final View mView;
public final TextView mIdView; public final TextView mCycleView;
public final TextView mContentView; public final TextView mTitleView;
public Task mItem; public Task mItem;
public ViewHolder(View view) { public ViewHolder(View view) {
super(view); super(view);
mView = view; mView = view;
mIdView = (TextView) view.findViewById(R.id.task_cycle); mCycleView = (TextView) view.findViewById(R.id.task_cycle);
mContentView = (TextView) view.findViewById(R.id.task_name); mTitleView = (TextView) view.findViewById(R.id.task_name);
} }
@Override @Override
public String toString() { public String toString() {
return super.toString() + " '" + mContentView.getText() + "'"; return super.toString() + " '" + mTitleView.getText() + "'";
} }
} }
} }

View File

@@ -57,19 +57,19 @@ public class TaskDataAccess {
return newTask; return newTask;
} }
public Cursor deleteTask(Cursor taskCursor) { /*public Cursor deleteTask(Cursor taskCursor) {
Task task = cursorToTask(taskCursor); Task task = cursorToTask(taskCursor);
long id = task.getId(); long id = task.getId();
//System.out.println("Task deleted with id: " + id); //System.out.println("Task deleted with id: " + id);
database.delete(DatabaseHelper.TASKS_TABLE_NAME, DatabaseHelper.COLUMN_ID database.delete(DatabaseHelper.TASKS_TABLE_NAME, DatabaseHelper.COLUMN_ID
+ " = " + id, null); + " = " + id, null);
return getAllTasksCursor(); return getAllTasksCursor();
} }*/
public List<Task> getAllTasks(long id) { public List<Task> getAllTasks(long id) {
List<Task> tasks = new ArrayList<>(); List<Task> tasks = new ArrayList<>();
Cursor cursor = id != -1 ? getAllTasksCursor(id) : getAllTasksCursor(); Cursor cursor = getAllTasksCursor(id);
cursor.moveToFirst(); cursor.moveToFirst();
while (!cursor.isAfterLast()) { while (!cursor.isAfterLast()) {
@@ -82,9 +82,27 @@ public class TaskDataAccess {
return tasks; return tasks;
} }
public Cursor getAllTasksCursor() { public int getTaskCount(long id) {
return database.query(DatabaseHelper.TASKS_TABLE_NAME, int taskCount = 0;
taskColumns, null, null, null, null, null); Cursor cursor = database.rawQuery(
"SELECT COUNT(*) " +
" FROM " + DatabaseHelper.TASKS_TABLE_NAME +
" WHERE " + DatabaseHelper.TASKS_COLUMN_LIST + " = " + id, null);
cursor.moveToFirst();
taskCount = cursor.getInt(0);
cursor.close();
return taskCount;
}
public int getTotalCycles(long id) {
int totalCycles = 0;
Cursor cursor = database.rawQuery(
"SELECT SUM(" + DatabaseHelper.TASKS_COLUMN_CYCLE + ") " +
" FROM " + DatabaseHelper.TASKS_TABLE_NAME +
" WHERE " + DatabaseHelper.TASKS_COLUMN_LIST + " = " + id, null);
cursor.moveToFirst();
totalCycles = cursor.getInt(0);
cursor.close();
return totalCycles;
} }
public Cursor getAllTasksCursor(long id) { public Cursor getAllTasksCursor(long id) {

View File

@@ -94,7 +94,7 @@ public class TaskListDataAccess {
" (SELECT COUNT(*) " + " (SELECT COUNT(*) " +
" FROM " + DatabaseHelper.TASKS_TABLE_NAME + " FROM " + DatabaseHelper.TASKS_TABLE_NAME +
" WHERE " + DatabaseHelper.TASKS_TABLE_NAME + "." + DatabaseHelper.TASKS_COLUMN_LIST + " = " + " WHERE " + DatabaseHelper.TASKS_TABLE_NAME + "." + DatabaseHelper.TASKS_COLUMN_LIST + " = " +
DatabaseHelper.TASKLIST_TABLE_NAME + "._id) AS " + DatabaseHelper.TASKLIST_COLUMN_TASK_COUNT + DatabaseHelper.TASKLIST_TABLE_NAME + "." + DatabaseHelper.COLUMN_ID + ") AS " + DatabaseHelper.TASKLIST_COLUMN_TASK_COUNT +
" FROM " + DatabaseHelper.TASKLIST_TABLE_NAME, " FROM " + DatabaseHelper.TASKLIST_TABLE_NAME,
null); null);
} }

View File

@@ -8,6 +8,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView;
import com.wismna.geoffroy.donext.R; import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.adapters.TaskAdapter; import com.wismna.geoffroy.donext.adapters.TaskAdapter;
@@ -58,17 +59,25 @@ public class TasksFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_task_list, container, false); View view = inflater.inflate(R.layout.fragment_task_list, container, false);
// Set the adapter // Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext(); Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view; RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.task_list_view);
recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.setLayoutManager(new LinearLayoutManager(context));
taskDataAccess = new TaskDataAccess(view.getContext()); taskDataAccess = new TaskDataAccess(view.getContext());
taskDataAccess.open(); taskDataAccess.open();
// Set total cycles
TextView totalCyclesView = (TextView) view.findViewById(R.id.total_task_cycles);
totalCyclesView.setText(String.valueOf(taskDataAccess.getTotalCycles(taskListId)));
// Set total count
TextView totalTasksView = (TextView) view.findViewById(R.id.total_task_count);
totalTasksView.setText(String.valueOf(taskDataAccess.getTaskCount(taskListId)));
// Set RecyclerView Adapter
TaskAdapter taskAdapter = new TaskAdapter(taskDataAccess.getAllTasks(taskListId), mListener); TaskAdapter taskAdapter = new TaskAdapter(taskDataAccess.getAllTasks(taskListId), mListener);
((RecyclerView) view).setAdapter(taskAdapter); recyclerView.setAdapter(taskAdapter);
}
return view; return view;
} }

View File

@@ -3,7 +3,6 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" > android:orientation="horizontal" >
<TextView <TextView
android:id="@+id/task_cycle" android:id="@+id/task_cycle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@@ -17,4 +16,9 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin" android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" /> android:textAppearance="?attr/textAppearanceListItem" />
<!--<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/task_list_delete"
android:onClick="onDeleteTask"/>-->
</LinearLayout> </LinearLayout>

View File

@@ -1,13 +1,28 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list" android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/total_task_cycles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/total_task_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<android.support.v7.widget.RecyclerView
android:id="@+id/task_list_view"
android:name="com.wismna.geoffroy.donext.activities.TaskFragment" android:name="com.wismna.geoffroy.donext.activities.TaskFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
android:layout_below="@id/total_task_cycles"
app:layoutManager="LinearLayoutManager" app:layoutManager="LinearLayoutManager"
tools:context=".fragments.TasksFragment" tools:context=".fragments.TasksFragment"
tools:listitem="@layout/fragment_task" /> tools:listitem="@layout/fragment_task" />
</RelativeLayout>