@Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   ProgramStageRow row = (ProgramStageRow) listView.getItemAtPosition(position);
   if (row instanceof ProgramStageEventRow) {
     ProgramStageEventRow eventRow = (ProgramStageEventRow) row;
     Event event = eventRow.getEvent();
     showDataEntryFragment(event, event.getProgramStageId());
   }
 }
  @Override
  public void onLoadFinished(
      Loader<ProgramOverviewFragmentForm> loader, ProgramOverviewFragmentForm data) {
    if (LOADER_ID == loader.getId()) {
      mForm = data;
      mProgressBar.setVisibility(View.GONE);
      if (mForm == null || mForm.getEnrollment() == null) {
        showNoActiveEnrollment();
        return;
      } else {
        enrollmentLayout.setVisibility(View.VISIBLE);
        missingEnrollmentLayout.setVisibility(View.GONE);
      }
      enrollmentDateLabel.setText(data.getDateOfEnrollmentLabel());
      enrollmentDateValue.setText(data.getDateOfEnrollmentValue());
      incidentDateLabel.setText(data.getIncidentDateLabel());
      incidentDateValue.setText(data.getIncidentDateValue());

      if (mForm.getEnrollment().getStatus().equals(Enrollment.COMPLETED)) {
        setCompleted();
      }

      if (mForm.getEnrollment().getStatus().equals(Enrollment.CANCELLED)) {
        setTerminated();
      }

      if (mForm.getEnrollment().getFollowup()) {
        setFollowupButton(true);
      }

      if (data.getAttribute1Label() == null || data.getAttribute1Value() == null) {
        attribute1Label.setVisibility(View.GONE);
        attribute1Value.setVisibility(View.GONE);
      } else {
        attribute1Label.setText(data.getAttribute1Label());
        attribute1Value.setText(data.getAttribute1Value());
      }

      if (data.getAttribute2Label() == null || data.getAttribute2Value() == null) {
        attribute2Label.setVisibility(View.GONE);
        attribute2Value.setVisibility(View.GONE);
      } else {
        attribute2Label.setText(data.getAttribute2Label());
        attribute2Value.setText(data.getAttribute2Value());
      }

      final Map<Long, FailedItem> failedEvents = getFailedEvents();
      List<Event> events = DataValueController.getEventsByEnrollment(data.getEnrollment().localId);
      Log.d(CLASS_TAG, "num failed events: " + failedEvents.size());
      boolean generateNextVisit = false;

      for (ProgramStageRow row : data.getProgramStageRows()) {
        if (row instanceof ProgramStageLabelRow) {
          ProgramStageLabelRow stageRow = (ProgramStageLabelRow) row;
          if (stageRow.getProgramStage().getRepeatable()) {
            stageRow.setButtonListener(this);
          }

          if (generateNextVisit) {
            int stageCount = 0;

            if (stageRow.getEventRows() != null) {

              for (ProgramStageEventRow eventRow : stageRow.getEventRows()) {
                stageCount++;
              }
            }
            if (stageCount < 1
                || stageRow
                    .getProgramStage()
                    .getRepeatable()) // should only be able to add more stages if stage is
                                      // repeatable
            stageRow.setButtonListener(this);

            generateNextVisit = false;
          }

          if (stageRow.getProgramStage().getAllowGenerateNextVisit()) {
            if (stageRow.getEventRows() != null) {
              for (ProgramStageEventRow eventRow : stageRow.getEventRows()) {
                if (eventRow.getEvent().getStatus().equals(Event.STATUS_COMPLETED))
                  generateNextVisit = true;
              }
            }
          }

        } else if (row instanceof ProgramStageEventRow) {
          final ProgramStageEventRow eventRow = (ProgramStageEventRow) row;

          if (failedEvents.containsKey(eventRow.getEvent().getLocalId())) {
            eventRow.setHasFailed(true);
            eventRow.setMessage(
                failedEvents.get(eventRow.getEvent().getLocalId()).getErrorMessage());
          } else if (eventRow.getEvent().getFromServer()) {
            eventRow.setSynchronized(true);
            eventRow.setMessage(getString(R.string.status_sent_description));
          } else if (!(eventRow.getEvent().getFromServer())) {
            eventRow.setSynchronized(false);
            eventRow.setMessage(getString(R.string.status_offline_description));
          }
        }
      }

      adapter.swapData(data.getProgramStageRows());
    }
  }