public boolean onContextItemSelected(MenuItem menuItem) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo(); contextSelection = info.position; switch (menuItem.getItemId()) { case CONTEXT_EDIT: Intent iAddTask = new Intent("ca.projectkarma.gradetrackr.activity.ADDTASK"); iAddTask.putExtra("idEdit_Item", tasks.get(contextSelection).getDatabaseID()); iAddTask.putExtra("id_Mode", EditMode.EDIT_MODE); startActivity(iAddTask); dataReset(); return true; case CONTEXT_DELETE: tasksDB.open(); Cursor cDelete = tasksDB.getTask(tasks.get(contextSelection).getDatabaseID()); String titleDelete = cDelete.getString(cDelete.getColumnIndex("taskTitle")); tasksDB.close(); new AlertDialog.Builder(this) .setTitle("Delete Task?") .setMessage("Are you sure you want to delete \"" + titleDelete + "\"?") .setPositiveButton( "Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tasksDB.open(); Cursor cDelete = tasksDB.getTask(tasks.get(contextSelection).getDatabaseID()); String titleDelete = cDelete.getString(cDelete.getColumnIndex("taskTitle")); tasksDB.deleteTask(tasks.get(contextSelection).getDatabaseID()); tasksDB.close(); Toast toast = Toast.makeText( context, titleDelete + " was deleted successfully.", Toast.LENGTH_SHORT); try { // center toast ((TextView) ((LinearLayout) toast.getView()).getChildAt(0)) .setGravity(Gravity.CENTER_HORIZONTAL); } catch (ClassCastException cce) { } toast.show(); dataReset(); } }) .setNegativeButton("Cancel", null) .show(); return true; default: return super.onContextItemSelected(menuItem); } }
public void dataReadToList() { tasksDB.open(); Cursor c = tasksDB.getAllTasks(); if (c.moveToFirst()) { do { // get ids of each tasks.add( new TaskData( c.getInt(c.getColumnIndex("_id")), c.getString(c.getColumnIndex("taskTitle")), c.getString(c.getColumnIndex("taskDateDue")), c.getString(c.getColumnIndex("taskDateDueTime")), context)); } while (c.moveToNext()); } Collections.sort(tasks, new TaskData()); // TODO: move aside already completed tasks or duedates that have // already passed (display in red font next to task? tasksDB.close(); }