@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);
  }
Example #2
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
      convertView =
          View.inflate(this.getContext(), R.layout.main_trainingschedule_overview_exercise, null);
      viewHolder = new ViewHolder();
      viewHolder.poseView =
          (ImageView) convertView.findViewById(R.id.main_trainingschedule_workout_type);
      viewHolder.nameView =
          (TextView) convertView.findViewById(R.id.main_trainingschedule_workout_name);

      convertView.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    Exercise exercise = this.exercises.get(position);
    viewHolder.poseView.setImageResource(this.exerciseIcons[exercise.getWorkoutType() - 1]);
    viewHolder.nameView.setText(exercise.getName());

    return convertView;
  }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

      // BROWSE STATES
      final int NOT_BROWSE = 0, BROWSE_WORKOUT = 1, WORKOUT_BROWSE = 2;

      if ((convertView == null)) {
        convertView = getLayoutInflater().inflate(R.layout.b_frag_exercise_list_item, null);
      }

      final SwipableLinearLayout swipableLinearLayout =
          (SwipableLinearLayout) convertView.findViewById(R.id.swipeLayoutHandle);
      swipableLinearLayout.setSwipeOffset(slideVal);
      swipableLinearLayout.setSwipeLayoutListener(listener);
      swipableLinearLayout.percentageToDragEnable(75f);

      final Exercise e = getItem(position);
      TextView nameTextView = (TextView) convertView.findViewById(R.id.browseMenuExerciseNameView);
      nameTextView.setText(e.getName());
      TextView muscleTextView =
          (TextView) convertView.findViewById(R.id.browseMenuExerciseMuscleView);
      muscleTextView.setText(e.getMuscleGroup());
      TextView equipmentTextView =
          (TextView) convertView.findViewById(R.id.browseMenuExerciseEquip);
      equipmentTextView.setText(e.getEquipment());

      if (swipableLinearLayout.getX() != 0) {
        swipableLinearLayout.setX(0f);

        if (mTextViewHandle != null) mTextViewHandle.setOpen(false);

        mTextViewHandle = null;
      }
      swipableLinearLayout.refreshIcon();

      swipableLinearLayout.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*
              Exercise exercise = new Exercise();
              exercise.setName(e.getName());
              exercise.setId(e.getId());
              //Stubs
              //exercise.instantiateStubMetrics();
              for (Metric m : e.getMetrics()){
                  Metric nm = new Metric();
                  nm.setType(m.getType());
                  nm.setMetricIntValue(m.getMetricIntValue());
                  exercise.addMetrics(nm);
              }

              WorkoutData.get(mContext).setToggledExerciseExplicit(e);//Sets this as the 'last' item added

              int circuitValue = WorkoutData.get(mContext).getStateCircuit();
              boolean circuitOpenStatus = WorkoutData.get(mContext).isStateCircuitOpen();
              //if circuit is open
              if (circuitOpenStatus) {
                  //addToOpenCircuit to that circuit
                  WorkoutData.get(mContext).addExerciseToOpenCircuit(exercise,
                         circuitValue);
                  //if circuit is closed
              } else if (!circuitOpenStatus) {
                  //add a closed circuit, with the exercise in it
                  WorkoutData.get(mContext).addClosedCircuit(exercise,
                          circuitValue);
              }
              //return to workspace
              */
              WorkoutData.get(mContext).swap(e);
              WorkoutData.get(mContext).setBrowseState(BROWSE_WORKOUT);
              Intent i = new Intent(mContext, WorkspaceActivity.class);
              startActivity(i);
            }
          });

      ImageButton delete = (ImageButton) convertView.findViewById(R.id.deleteButton);
      delete.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              WorkoutData.get(mContext).exerciseRemoved(e.getId());
              DatabaseWrapper db = new DatabaseWrapper();
              db.deleteExerciseInExerciseTable(e.getId());
              mExercises.remove(e);
              notifyDataSetChanged();
              /*
              if(WorkoutData.get(mContext).isAnExerciseToggled()){
                  if (WorkoutData.get(mContext).getToggledExercise() == e){
                      WorkoutData.get(mContext).clearToggledExercise();
                  }
              }
              */
            }
          });

      return convertView;
    }