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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
@@ -175,6 +176,12 @@ public class MainActivity extends AppCompatActivity implements NewTaskFragment.N
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
public void onListFragmentInteraction(Task item) {

View File

@@ -37,7 +37,7 @@ public class TaskListActivity extends AppCompatActivity {
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) {
@SuppressWarnings("unchecked")
EditText editText = (EditText) findViewById(R.id.new_task_list_name);
@@ -48,6 +48,7 @@ public class TaskListActivity extends AppCompatActivity {
updateCreateButtonEnabled();
}
/** Will be called when the delete Task List button is clicked */
public void onDeleteTaskList(View view) {
@SuppressWarnings("unchecked")
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
* 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 OnListFragmentInteractionListener mListener;
@@ -30,14 +30,17 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_task, parent, false);
return new ViewHolder(view);
ViewHolder holder = new ViewHolder(view);
holder.mTitleView.setOnClickListener(TaskAdapter.this);
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(String.valueOf(holder.mItem.getCycle()));
holder.mContentView.setText(holder.mItem.getName());
holder.mCycleView.setText(String.valueOf(holder.mItem.getCycle()));
holder.mTitleView.setText(holder.mItem.getName());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
@@ -56,6 +59,13 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
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) {
mValues.add(position, item);
notifyItemInserted(position);
@@ -69,20 +79,20 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public final TextView mCycleView;
public final TextView mTitleView;
public Task mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.task_cycle);
mContentView = (TextView) view.findViewById(R.id.task_name);
mCycleView = (TextView) view.findViewById(R.id.task_cycle);
mTitleView = (TextView) view.findViewById(R.id.task_name);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
return super.toString() + " '" + mTitleView.getText() + "'";
}
}
}

View File

@@ -57,19 +57,19 @@ public class TaskDataAccess {
return newTask;
}
public Cursor deleteTask(Cursor taskCursor) {
/*public Cursor deleteTask(Cursor taskCursor) {
Task task = cursorToTask(taskCursor);
long id = task.getId();
//System.out.println("Task deleted with id: " + id);
database.delete(DatabaseHelper.TASKS_TABLE_NAME, DatabaseHelper.COLUMN_ID
+ " = " + id, null);
return getAllTasksCursor();
}
}*/
public List<Task> getAllTasks(long id) {
List<Task> tasks = new ArrayList<>();
Cursor cursor = id != -1 ? getAllTasksCursor(id) : getAllTasksCursor();
Cursor cursor = getAllTasksCursor(id);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
@@ -82,9 +82,27 @@ public class TaskDataAccess {
return tasks;
}
public Cursor getAllTasksCursor() {
return database.query(DatabaseHelper.TASKS_TABLE_NAME,
taskColumns, null, null, null, null, null);
public int getTaskCount(long id) {
int taskCount = 0;
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) {

View File

@@ -94,7 +94,7 @@ public class TaskListDataAccess {
" (SELECT COUNT(*) " +
" FROM " + DatabaseHelper.TASKS_TABLE_NAME +
" 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,
null);
}

View File

@@ -8,6 +8,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.wismna.geoffroy.donext.R;
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);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
Context context = view.getContext();
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.open();
TaskAdapter taskAdapter = new TaskAdapter(taskDataAccess.getAllTasks(taskListId), mListener);
((RecyclerView) view).setAdapter(taskAdapter);
}
taskDataAccess = new TaskDataAccess(view.getContext());
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);
recyclerView.setAdapter(taskAdapter);
return view;
}

View File

@@ -2,8 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="horizontal" >
<TextView
android:id="@+id/task_cycle"
android:layout_width="wrap_content"
@@ -17,4 +16,9 @@
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
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>

View File

@@ -1,13 +1,28 @@
<?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:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="com.wismna.geoffroy.donext.activities.TaskFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.TasksFragment"
tools:listitem="@layout/fragment_task" />
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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_below="@id/total_task_cycles"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.TasksFragment"
tools:listitem="@layout/fragment_task" />
</RelativeLayout>