Exemple #1
0
  private Message createMsgFromCursor(MatrixCursor mCursor) {
    Message msg = new Message();

    msg.setUserId(mCursor.getInt(1));
    msg.setText(mCursor.getString(2));
    msg.setTimestamp(formatting.formatStringToDate(mCursor.getString(3), msg.FORMAT));
    return msg;
  }
  @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);
  }
  private void populateChallengesList() {
    Log.d(TAG, "populating challenger list");
    challenges.moveToFirst();
    Log.d(
        TAG,
        "first challenger: "
            + challenges.getString(challenges.getColumnIndex(CHALLENGE_SOURCE_HANDLE_COL)));
    String[] from = {CHALLENGE_SOURCE_HANDLE_COL};
    int[] to = {R.id.challenger_text_view};

    SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this, R.layout.challenger_text_view, challenges, from, to);
    ListView lv = (ListView) findViewById(R.id.view_challenges);
    lv.setAdapter(adapter);
    final CursorWrapper c = new CursorWrapper(challenges);

    lv.setOnItemClickListener(
        new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            // Move the cursor to the selected item
            c.moveToPosition(pos);

            int cid = c.getInt(c.getColumnIndex("_id"));
            int sourceId = c.getInt(c.getColumnIndex(CHALLENGE_SOURCE_ID_COL));
            String sourceHandle = c.getString(c.getColumnIndex(CHALLENGE_SOURCE_HANDLE_COL));
            int targetId = c.getInt(c.getColumnIndex(CHALLENGE_TARGET_COL));
            challengeDialog(cid, sourceId, sourceHandle, targetId);
          }
        });
  }
 // 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);
   }
 }