From f7eb5ca0180f448787ab831011f54412ab560d5b Mon Sep 17 00:00:00 2001 From: geoffroy Date: Fri, 27 Nov 2015 23:49:50 -0500 Subject: [PATCH] Tasks are shown in the tabs (cycles + names) --- DoNExt/app/build.gradle | 1 + .../donext/activities/MainActivity.java | 54 +-------- .../donext/activities/TaskListActivity.java | 2 + .../geoffroy/donext/adapters/TaskAdapter.java | 78 +++++++++++++ .../donext/adapters/TaskCursorAdapter.java | 45 -------- .../donext/database/TaskDataAccess.java | 9 +- .../donext/fragments/TaskFragment.java | 106 ++++++++++++++++++ .../app/src/main/res/layout/fragment_main.xml | 21 ---- .../app/src/main/res/layout/fragment_task.xml | 20 ++++ .../main/res/layout/fragment_task_list.xml | 13 +++ DoNExt/app/src/main/res/values/dimens.xml | 1 + 11 files changed, 234 insertions(+), 116 deletions(-) create mode 100644 DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskAdapter.java delete mode 100644 DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskCursorAdapter.java create mode 100644 DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TaskFragment.java delete mode 100644 DoNExt/app/src/main/res/layout/fragment_main.xml create mode 100644 DoNExt/app/src/main/res/layout/fragment_task.xml create mode 100644 DoNExt/app/src/main/res/layout/fragment_task_list.xml diff --git a/DoNExt/app/build.gradle b/DoNExt/app/build.gradle index bebdd85..6d87ad6 100644 --- a/DoNExt/app/build.gradle +++ b/DoNExt/app/build.gradle @@ -25,4 +25,5 @@ dependencies { compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.android.support:support-v4:23.1.1' + compile 'com.android.support:recyclerview-v7:23.1.1' } diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java index 330e6de..9be2280 100644 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java @@ -12,30 +12,26 @@ import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; -import android.widget.TextView; import com.wismna.geoffroy.donext.R; -import com.wismna.geoffroy.donext.adapters.TaskCursorAdapter; +import com.wismna.geoffroy.donext.dao.Task; import com.wismna.geoffroy.donext.dao.TaskList; import com.wismna.geoffroy.donext.database.TaskDataAccess; import com.wismna.geoffroy.donext.database.TaskListDataAccess; import com.wismna.geoffroy.donext.fragments.NewTaskFragment; +import com.wismna.geoffroy.donext.fragments.TaskFragment; import java.util.List; -public class MainActivity extends AppCompatActivity implements NewTaskFragment.NewTaskListener { +public class MainActivity extends AppCompatActivity implements NewTaskFragment.NewTaskListener, TaskFragment.OnListFragmentInteractionListener { protected TaskDataAccess taskDataAccess; - protected TaskCursorAdapter adapter; /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a @@ -171,47 +167,9 @@ public class MainActivity extends AppCompatActivity implements NewTaskFragment.N newTaskFragment.show(manager, "Create new task"); } - /** - * A placeholder fragment containing a simple view. - */ - public static class PlaceholderFragment extends Fragment { - /** - * The fragment argument representing the section number for this - * fragment. - */ - private static final String ARG_SECTION_NUMBER = "section_number"; + @Override + public void onListFragmentInteraction(Task item) { - public PlaceholderFragment() { - } - - /** - * Returns a new instance of this fragment for the given section - * number. - */ - public static PlaceholderFragment newInstance(int sectionNumber) { - PlaceholderFragment fragment = new PlaceholderFragment(); - Bundle args = new Bundle(); - args.putInt(ARG_SECTION_NUMBER, sectionNumber); - fragment.setArguments(args); - return fragment; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_main, container, false); - TextView textView = (TextView) rootView.findViewById(R.id.section_label); - textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); - - // TODO: implement task list - - /*ListView listView = (ListView) rootView.findViewById(android.R.id.list); - - adapter = new TaskCursorAdapter( - rootView.getContext(), R.layout.item_task, taskDataAccess.getAllTasksCursor(), 0); - listView.setAdapter(adapter);*/ - return rootView; - } } /** @@ -228,7 +186,7 @@ public class MainActivity extends AppCompatActivity implements NewTaskFragment.N public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). - return PlaceholderFragment.newInstance(position + 1); + return TaskFragment.newInstance(taskLists.get(position).getId()); } @Override diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/TaskListActivity.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/TaskListActivity.java index 61f370b..6895f86 100644 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/TaskListActivity.java +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/TaskListActivity.java @@ -14,6 +14,8 @@ import com.wismna.geoffroy.donext.R; import com.wismna.geoffroy.donext.adapters.TaskListCursorAdapter; import com.wismna.geoffroy.donext.database.TaskListDataAccess; +// TODO: create a fragment +// TODO: replace ListView with RecycleView public class TaskListActivity extends AppCompatActivity { private TaskListDataAccess dataAccess; private TaskListCursorAdapter adapter; diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskAdapter.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskAdapter.java new file mode 100644 index 0000000..ebae310 --- /dev/null +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskAdapter.java @@ -0,0 +1,78 @@ +package com.wismna.geoffroy.donext.adapters; + +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.dao.Task; +import com.wismna.geoffroy.donext.fragments.TaskFragment.OnListFragmentInteractionListener; + +import java.util.List; + +/** + * {@link RecyclerView.Adapter} that can display a {@link Task} and makes a call to the + * specified {@link OnListFragmentInteractionListener}. + * TODO: Replace the implementation with code for your data type. + */ +public class TaskAdapter extends RecyclerView.Adapter { + + private final List mValues; + private final OnListFragmentInteractionListener mListener; + + public TaskAdapter(List items, OnListFragmentInteractionListener listener) { + mValues = items; + mListener = listener; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.fragment_task, parent, false); + return new ViewHolder(view); + } + + @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.mView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (null != mListener) { + // Notify the active callbacks interface (the activity, if the + // fragment is attached to one) that an item has been selected. + mListener.onListFragmentInteraction(holder.mItem); + } + } + }); + } + + @Override + public int getItemCount() { + return mValues.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + public final View mView; + public final TextView mIdView; + public final TextView mContentView; + 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); + } + + @Override + public String toString() { + return super.toString() + " '" + mContentView.getText() + "'"; + } + } +} diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskCursorAdapter.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskCursorAdapter.java deleted file mode 100644 index e410295..0000000 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/TaskCursorAdapter.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.wismna.geoffroy.donext.adapters; - -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; - -import com.wismna.geoffroy.donext.R; -import com.wismna.geoffroy.donext.database.DatabaseHelper; - -/** - * Created by geoffroy on 15-11-27. - */ -public class TaskCursorAdapter extends ResourceCursorAdapter { - public TaskCursorAdapter(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, 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 taskListName = (TextView) view.findViewById(R.id.task_name); - // Extract properties from cursor - String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.TASKS_COLUMN_NAME)); - // Populate fields with extracted properties - taskListName.setText(name); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - return super.getView(position, convertView, parent); - } -} diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/database/TaskDataAccess.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/database/TaskDataAccess.java index cccc0a4..a2e1eee 100644 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/database/TaskDataAccess.java +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/database/TaskDataAccess.java @@ -59,10 +59,10 @@ public class TaskDataAccess { return getAllTasksCursor(); } - public List getAllTasks() { + public List getAllTasks(long id) { List tasks = new ArrayList<>(); - Cursor cursor = getAllTasksCursor(); + Cursor cursor = id != -1 ? getAllTasksCursor(id) : getAllTasksCursor(); cursor.moveToFirst(); while (!cursor.isAfterLast()) { @@ -80,6 +80,11 @@ public class TaskDataAccess { taskColumns, null, null, null, null, null); } + public Cursor getAllTasksCursor(long id) { + return database.query(DatabaseHelper.TASKS_TABLE_NAME, + taskColumns, DatabaseHelper.TASKS_COLUMN_LIST + " = " + id, null, null, null, null); + } + private Task cursorToTask(Cursor cursor) { Task task = new Task(); task.setId(cursor.getLong(0)); diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TaskFragment.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TaskFragment.java new file mode 100644 index 0000000..4cbda44 --- /dev/null +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TaskFragment.java @@ -0,0 +1,106 @@ +package com.wismna.geoffroy.donext.fragments; + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.wismna.geoffroy.donext.R; +import com.wismna.geoffroy.donext.adapters.TaskAdapter; +import com.wismna.geoffroy.donext.dao.Task; +import com.wismna.geoffroy.donext.database.TaskDataAccess; + +/** + * A fragment representing a list of Items. + *

+ * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener} + * interface. + */ +public class TaskFragment extends Fragment { + + private TaskDataAccess taskDataAccess; + private static final String TASK_LIST_ID = "task_list_id"; + private long taskListId = -1; + private OnListFragmentInteractionListener mListener; + + /** + * Mandatory empty constructor for the fragment manager to instantiate the + * fragment (e.g. upon screen orientation changes). + */ + public TaskFragment() { + } + + @SuppressWarnings("unused") + public static TaskFragment newInstance(long taskListId) { + TaskFragment fragment = new TaskFragment(); + Bundle args = new Bundle(); + args.putLong(TASK_LIST_ID, taskListId); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (getArguments() != null) { + taskListId = getArguments().getLong(TASK_LIST_ID); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + 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; + + recyclerView.setLayoutManager(new LinearLayoutManager(context)); + + taskDataAccess = new TaskDataAccess(view.getContext()); + taskDataAccess.open(); + TaskAdapter taskAdapter = new TaskAdapter(taskDataAccess.getAllTasks(taskListId), mListener); + ((RecyclerView) view).setAdapter(taskAdapter); + } + return view; + } + + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof OnListFragmentInteractionListener) { + mListener = (OnListFragmentInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnListFragmentInteractionListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; + } + + /** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in this fragment to be communicated + * to the activity and potentially other fragments contained in that + * activity. + *

+ * See the Android Training lesson Communicating with Other Fragments for more information. + */ + public interface OnListFragmentInteractionListener { + void onListFragmentInteraction(Task item); + } +} diff --git a/DoNExt/app/src/main/res/layout/fragment_main.xml b/DoNExt/app/src/main/res/layout/fragment_main.xml deleted file mode 100644 index 2c77fd7..0000000 --- a/DoNExt/app/src/main/res/layout/fragment_main.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - diff --git a/DoNExt/app/src/main/res/layout/fragment_task.xml b/DoNExt/app/src/main/res/layout/fragment_task.xml new file mode 100644 index 0000000..5a8f2ed --- /dev/null +++ b/DoNExt/app/src/main/res/layout/fragment_task.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/DoNExt/app/src/main/res/layout/fragment_task_list.xml b/DoNExt/app/src/main/res/layout/fragment_task_list.xml new file mode 100644 index 0000000..526a62e --- /dev/null +++ b/DoNExt/app/src/main/res/layout/fragment_task_list.xml @@ -0,0 +1,13 @@ + + diff --git a/DoNExt/app/src/main/res/values/dimens.xml b/DoNExt/app/src/main/res/values/dimens.xml index cef3abc..7d9415a 100644 --- a/DoNExt/app/src/main/res/values/dimens.xml +++ b/DoNExt/app/src/main/res/values/dimens.xml @@ -4,4 +4,5 @@ 16dp 16dp 8dp + 16dp