コード例 #1
0
  private String getCurrentPath() {
    FormController formController = Collect.getInstance().getFormController();
    FormIndex index = formController.getFormIndex();
    // move to enclosing group...
    index = formController.stepIndexOut(index);

    String path = "";
    while (index != null) {

      path =
          formController.getCaptionPrompt(index).getLongText()
              + " ("
              + (formController.getCaptionPrompt(index).getMultiplicity() + 1)
              + ") > "
              + path;

      index = formController.stepIndexOut(index);
    }
    // return path?
    return path.substring(0, path.length() - 2);
  }
コード例 #2
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hierarchy_layout);

    FormController formController = Collect.getInstance().getFormController();

    // We use a static FormEntryController to make jumping faster.
    mStartIndex = formController.getFormIndex();

    setTitle(getString(R.string.app_name) + " > " + formController.getFormTitle());

    mPath = (TextView) findViewById(R.id.pathtext);

    jumpPreviousButton = (Button) findViewById(R.id.jumpPreviousButton);
    jumpPreviousButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            Collect.getInstance()
                .getActivityLogger()
                .logInstanceAction(this, "goUpLevelButton", "click");
            goUpLevel();
          }
        });

    Button jumpBeginningButton = (Button) findViewById(R.id.jumpBeginningButton);
    jumpBeginningButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            Collect.getInstance()
                .getActivityLogger()
                .logInstanceAction(this, "jumpToBeginning", "click");
            Collect.getInstance()
                .getFormController()
                .jumpToIndex(FormIndex.createBeginningOfFormIndex());
            setResult(RESULT_OK);
            finish();
          }
        });

    Button jumpEndButton = (Button) findViewById(R.id.jumpEndButton);
    jumpEndButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "jumpToEnd", "click");
            Collect.getInstance().getFormController().jumpToIndex(FormIndex.createEndOfFormIndex());
            setResult(RESULT_OK);
            finish();
          }
        });

    refreshView();

    // kinda slow, but works.
    // this scrolls to the last question the user was looking at
    if (getListAdapter() != null && getListView() != null) {
      getListView()
          .post(
              new Runnable() {
                @Override
                public void run() {
                  int position = 0;
                  for (int i = 0; i < getListAdapter().getCount(); i++) {
                    HierarchyElement he = (HierarchyElement) getListAdapter().getItem(i);
                    if (mStartIndex.equals(he.getFormIndex())) {
                      position = i;
                      break;
                    }
                  }
                  getListView().setSelection(position);
                }
              });
    }
  }
コード例 #3
0
  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());
    }
  }