예제 #1
0
  @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);
  }
예제 #2
0
 // checks if any exercises have been removed and updates the global list accordingly
 // also rewrites the workouts file on the device
 private void checkRemoved() {
   ArrayList<Integer> positions = adapter.getCursorPositions();
   ArrayList<Exercise> newExercises = new ArrayList<Exercise>();
   if (cursor != null) {
     cursor.moveToFirst();
     for (int i = 0; i < positions.size(); i++) {
       int cursorPosition = positions.get(i);
       cursor.moveToPosition(cursorPosition);
       String c = cursor.getString(1);
       newExercises.add(workout.getExercise(c));
     }
     workout.setExercises(newExercises);
   }
 }