@Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    HierarchyElement h = (HierarchyElement) l.getItemAtPosition(position);
    FormIndex index = h.getFormIndex();
    if (index == null) {
      goUpLevel();
      return;
    }

    switch (h.getType()) {
      case EXPANDED:
        Collect.getInstance()
            .getActivityLogger()
            .logInstanceAction(this, "onListItemClick", "COLLAPSED", h.getFormIndex());
        h.setType(COLLAPSED);
        ArrayList<HierarchyElement> children = h.getChildren();
        for (int i = 0; i < children.size(); i++) {
          formList.remove(position + 1);
        }
        h.setIcon(getResources().getDrawable(R.drawable.expander_ic_minimized));
        h.setColor(Color.WHITE);
        break;
      case COLLAPSED:
        Collect.getInstance()
            .getActivityLogger()
            .logInstanceAction(this, "onListItemClick", "EXPANDED", h.getFormIndex());
        h.setType(EXPANDED);
        ArrayList<HierarchyElement> children1 = h.getChildren();
        for (int i = 0; i < children1.size(); i++) {
          Log.i(t, "adding child: " + children1.get(i).getFormIndex());
          formList.add(position + 1 + i, children1.get(i));
        }
        h.setIcon(getResources().getDrawable(R.drawable.expander_ic_maximized));
        h.setColor(Color.WHITE);
        break;
      case QUESTION:
        Collect.getInstance()
            .getActivityLogger()
            .logInstanceAction(this, "onListItemClick", "QUESTION-JUMP", index);
        Collect.getInstance().getFormController().jumpToIndex(index);
        if (Collect.getInstance().getFormController().indexIsInFieldList()) {
          try {
            Collect.getInstance().getFormController().stepToPreviousScreenEvent();
          } catch (JavaRosaException e) {
            Log.e(t, e.getMessage(), e);
            createErrorDialog(e.getCause().getMessage());
            return;
          }
        }
        setResult(RESULT_OK);
        finish();
        return;
      case CHILD:
        Collect.getInstance()
            .getActivityLogger()
            .logInstanceAction(this, "onListItemClick", "REPEAT-JUMP", h.getFormIndex());
        Collect.getInstance().getFormController().jumpToIndex(h.getFormIndex());
        setResult(RESULT_OK);
        refreshView();
        return;
    }

    // Should only get here if we've expanded or collapsed a group
    HierarchyListAdapter itla = new HierarchyListAdapter(this);
    itla.setListItems(formList);
    setListAdapter(itla);
    getListView().setSelection(position);
  }
  public void refreshView() {
    try {
      FormController formController = Collect.getInstance().getFormController();
      // Record the current index so we can return to the same place if the user hits 'back'.
      currentIndex = formController.getFormIndex();

      // If we're not at the first level, we're inside a repeated group so we want to only display
      // everything enclosed within that group.
      String contextGroupRef = "";
      formList = new ArrayList<HierarchyElement>();

      // If we're currently at a repeat node, record the name of the node and step to the next
      // node to display.
      if (formController.getEvent() == FormEntryController.EVENT_REPEAT) {
        contextGroupRef = formController.getFormIndex().getReference().toString(true);
        formController.stepToNextEvent(FormController.STEP_INTO_GROUP);
      } else {
        FormIndex startTest = formController.stepIndexOut(currentIndex);
        // If we have a 'group' tag, we want to step back until we hit a repeat or the
        // beginning.
        while (startTest != null
            && formController.getEvent(startTest) == FormEntryController.EVENT_GROUP) {
          startTest = formController.stepIndexOut(startTest);
        }
        if (startTest == null) {
          // check to see if the question is at the first level of the hierarchy. If it is,
          // display the root level from the beginning.
          formController.jumpToIndex(FormIndex.createBeginningOfFormIndex());
        } else {
          // otherwise we're at a repeated group
          formController.jumpToIndex(startTest);
        }

        // now test again for repeat. This should be true at this point or we're at the
        // beginning
        if (formController.getEvent() == FormEntryController.EVENT_REPEAT) {
          contextGroupRef = formController.getFormIndex().getReference().toString(true);
          formController.stepToNextEvent(FormController.STEP_INTO_GROUP);
        }
      }

      int event = formController.getEvent();
      if (event == FormEntryController.EVENT_BEGINNING_OF_FORM) {
        // The beginning of form has no valid prompt to display.
        formController.stepToNextEvent(FormController.STEP_INTO_GROUP);
        contextGroupRef =
            formController.getFormIndex().getReference().getParentRef().toString(true);
        mPath.setVisibility(View.GONE);
        jumpPreviousButton.setEnabled(false);
      } else {
        mPath.setVisibility(View.VISIBLE);
        mPath.setText(getCurrentPath());
        jumpPreviousButton.setEnabled(true);
      }

      // Refresh the current event in case we did step forward.
      event = formController.getEvent();

      // Big change from prior implementation:
      //
      // The ref strings now include the instance number designations
      // i.e., [0], [1], etc. of the repeat groups (and also [1] for
      // non-repeat elements).
      //
      // The contextGroupRef is now also valid for the top-level form.
      //
      // The repeatGroupRef is null if we are not skipping a repeat
      // section.
      //
      String repeatGroupRef = null;

      event_search:
      while (event != FormEntryController.EVENT_END_OF_FORM) {

        // get the ref to this element
        String currentRef = formController.getFormIndex().getReference().toString(true);

        // retrieve the current group
        String curGroup = (repeatGroupRef == null) ? contextGroupRef : repeatGroupRef;

        if (!currentRef.startsWith(curGroup)) {
          // We have left the current group
          if (repeatGroupRef == null) {
            // We are done.
            break event_search;
          } else {
            // exit the inner repeat group
            repeatGroupRef = null;
          }
        }

        if (repeatGroupRef != null) {
          // We're in a repeat group within the one we want to list
          // skip this question/group/repeat and move to the next index.
          event = formController.stepToNextEvent(FormController.STEP_INTO_GROUP);
          continue;
        }

        switch (event) {
          case FormEntryController.EVENT_QUESTION:
            FormEntryPrompt fp = formController.getQuestionPrompt();
            String label = fp.getLongText();
            if (!fp.isReadOnly() || (label != null && label.length() > 0)) {
              // show the question if it is an editable field.
              // or if it is read-only and the label is not blank.
              formList.add(
                  new HierarchyElement(
                      fp.getLongText(),
                      fp.getAnswerText(),
                      null,
                      Color.WHITE,
                      QUESTION,
                      fp.getIndex()));
            }
            break;
          case FormEntryController.EVENT_GROUP:
            // ignore group events
            break;
          case FormEntryController.EVENT_PROMPT_NEW_REPEAT:
            // this would display the 'add new repeat' dialog
            // ignore it.
            break;
          case FormEntryController.EVENT_REPEAT:
            FormEntryCaption fc = formController.getCaptionPrompt();
            // push this repeat onto the stack.
            repeatGroupRef = currentRef;
            // Because of the guard conditions above, we will skip
            // everything until we exit this repeat.
            //
            // Note that currentRef includes the multiplicity of the
            // repeat (e.g., [0], [1], ...), so every repeat will be
            // detected as different and reach this case statement.
            // Only the [0] emits the repeat header.
            // Every one displays the descend-into action element.

            if (fc.getMultiplicity() == 0) {
              // Display the repeat header for the group.
              HierarchyElement group =
                  new HierarchyElement(
                      fc.getLongText(),
                      null,
                      getResources().getDrawable(R.drawable.expander_ic_minimized),
                      Color.WHITE,
                      COLLAPSED,
                      fc.getIndex());
              formList.add(group);
            }
            // Add this group name to the drop down list for this repeating group.
            HierarchyElement h = formList.get(formList.size() - 1);
            h.addChild(
                new HierarchyElement(
                    mIndent + fc.getLongText() + " " + (fc.getMultiplicity() + 1),
                    null,
                    null,
                    Color.WHITE,
                    CHILD,
                    fc.getIndex()));
            break;
        }
        event = formController.stepToNextEvent(FormController.STEP_INTO_GROUP);
      }

      HierarchyListAdapter itla = new HierarchyListAdapter(this);
      itla.setListItems(formList);
      setListAdapter(itla);

      // set the controller back to the current index in case the user hits 'back'
      formController.jumpToIndex(currentIndex);
    } catch (Exception e) {
      Log.e(t, e.getMessage(), e);
      createErrorDialog(e.getMessage());
    }
  }