@Override
  public void onEdit(Exercise exercise, int position) {
    if (position == -1) {
      for (Exercise e : workout.getExercises()) {
        String name = e.getName();
        if (name.equals(exercise.getName())) {
          Toast toast = Toast.makeText(this, "Exercise already exists!", Toast.LENGTH_SHORT);
          toast.setGravity(Gravity.CENTER, 0, 0);
          toast.show();
          return;
        }
      }
    }
    ArrayList<Integer> positions = adapter.getCursorPositions();
    MatrixCursor newcursor = new MatrixCursor(new String[] {"_id", "name"});
    // int lastCursorPosition = cursor.getPosition();
    // copy over old cursor items to the new one.
    if (cursor != null) {
      cursor.moveToFirst();
      for (int i = 0; i < positions.size(); i++) {
        int cursorPosition = positions.get(i);
        int listPosition = adapter.getListPosition(positions.get(i));
        // dont add to new cursor if item has been removed.
        if (listPosition == DragSortCursorAdapter.REMOVED) continue;
        cursor.moveToPosition(cursorPosition);
        String c = cursor.getString(1);
        // if its not a new exercise, check to see if the current cursor list position mapping
        // matches the one we are working with. If yes, set c to be the new exercise.
        if (position >= 0) {
          if (position == listPosition) {
            c = exercise.getName();
          }
        }
        newcursor.newRow().add(listPosition).add(c);
      }
    }
    // add the new row
    if (position < 0 && exercise != null) {
      newcursor.newRow().add(newcursor.getCount()).add(exercise.getName());
    }

    ArrayList<Exercise> newExercises = new ArrayList<Exercise>();
    newcursor.moveToFirst();
    for (int i = 0; i < newcursor.getCount(); i++) {
      String ename = newcursor.getString(1);
      Exercise e = workout.getExercise(ename);
      if (e != null) newExercises.add(e);
      else if (i == newcursor.getCount() - 1) newExercises.add(exercise);
      if (!newcursor.moveToNext()) break;
    }

    adapter.changeCursor(newcursor);
    cursor = newcursor;

    // modify workout to reflect the change
    workout.setExercises(newExercises);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_workout);

    Bundle bundle = this.getIntent().getExtras();
    position = bundle.getInt("position");

    editNameField = (EditText) findViewById(R.id.workoutNameField);
    addExerciseButton = (Button) findViewById(R.id.addExerciseButton);

    if (position >= 0) {
      workout = WorkoutList.getInstance().getWorkout(position);
      editNameField.setText(workout.getName());
    } else {
      workout = new Workout();
    }

    dslv = (DragSortListView) findViewById(R.id.exerciseList);

    String[] cols = {"name"};
    int[] ids = {R.id.text};
    adapter = new MyAdapter(this, R.layout.list_item_click_remove, null, cols, ids, 0);
    dslv.setAdapter(adapter);

    if (workout != null) {
      // populate the list of exercises
      cursor = new MatrixCursor(new String[] {"_id", "name"});
      int size = workout.getExercises().size();
      for (int i = 0; i < size; i++) {
        cursor.newRow().add(i).add(workout.getExercises().get(i).getName());
      }
      adapter.changeCursor(cursor);
    }

    setUpButtonListeners();
    setUpTextWatcher();
  }