public SelectOneAutoAdvanceWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    LayoutInflater inflater = LayoutInflater.from(getContext());

    // SurveyCTO-added support for dynamic select content (from .csv files)
    XPathFuncExpr xPathFuncExpr =
        ExternalDataUtil.getSearchXPathExpression(prompt.getAppearanceHint());
    if (xPathFuncExpr != null) {
      mItems = ExternalDataUtil.populateExternalChoices(prompt, xPathFuncExpr);
    } else {
      mItems = prompt.getSelectChoices();
    }

    buttons = new ArrayList<RadioButton>();
    listener = (AdvanceToNextListener) context;

    String s = null;
    if (prompt.getAnswerValue() != null) {
      s = ((Selection) prompt.getAnswerValue().getValue()).getValue();
    }

    // use this for recycle
    Bitmap b =
        BitmapFactory.decodeResource(getContext().getResources(), R.drawable.expander_ic_right);

    if (mItems != null) {
      for (int i = 0; i < mItems.size(); i++) {

        RelativeLayout thisParentLayout =
            (RelativeLayout) inflater.inflate(R.layout.quick_select_layout, null);

        LinearLayout questionLayout = (LinearLayout) thisParentLayout.getChildAt(0);
        ImageView rightArrow = (ImageView) thisParentLayout.getChildAt(1);

        RadioButton r = new RadioButton(getContext());
        r.setText(prompt.getSelectChoiceText(mItems.get(i)));
        r.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
        r.setTag(Integer.valueOf(i));
        r.setId(QuestionWidget.newUniqueId());
        r.setEnabled(!prompt.isReadOnly());
        r.setFocusable(!prompt.isReadOnly());

        rightArrow.setImageBitmap(b);

        buttons.add(r);

        if (mItems.get(i).getValue().equals(s)) {
          r.setChecked(true);
        }

        r.setOnCheckedChangeListener(this);

        String audioURI = null;
        audioURI =
            prompt.getSpecialFormSelectChoiceText(mItems.get(i), FormEntryCaption.TEXT_FORM_AUDIO);

        String imageURI;
        if (mItems.get(i) instanceof ExternalSelectChoice) {
          imageURI = ((ExternalSelectChoice) mItems.get(i)).getImage();
        } else {
          imageURI =
              prompt.getSpecialFormSelectChoiceText(
                  mItems.get(i), FormEntryCaption.TEXT_FORM_IMAGE);
        }

        String videoURI = null;
        videoURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "video");

        String bigImageURI = null;
        bigImageURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "big-image");

        MediaLayout mediaLayout = new MediaLayout(getContext());
        mediaLayout.setAVT(prompt.getIndex(), "", r, audioURI, imageURI, videoURI, bigImageURI);

        if (i != mItems.size() - 1) {
          // Last, add the dividing line (except for the last element)
          ImageView divider = new ImageView(getContext());
          divider.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
          mediaLayout.addDivider(divider);
        }
        questionLayout.addView(mediaLayout);
        addView(thisParentLayout);
      }
    }
  }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public SelectMultiWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);
    mPrompt = prompt;
    mCheckboxes = new ArrayList<CheckBox>();

    // SurveyCTO-added support for dynamic select content (from .csv files)
    XPathFuncExpr xPathFuncExpr =
        ExternalDataUtil.getSearchXPathExpression(prompt.getAppearanceHint());
    if (xPathFuncExpr != null) {
      mItems = ExternalDataUtil.populateExternalChoices(prompt, xPathFuncExpr);
    } else {
      mItems = prompt.getSelectChoices();
    }

    setOrientation(LinearLayout.VERTICAL);

    Vector<Selection> ve = new Vector<Selection>();
    if (prompt.getAnswerValue() != null) {
      ve = (Vector<Selection>) prompt.getAnswerValue().getValue();
    }

    if (mItems != null) {
      for (int i = 0; i < mItems.size(); i++) {
        // no checkbox group so id by answer + offset
        CheckBox c = new CheckBox(getContext());
        c.setTag(Integer.valueOf(i));
        c.setId(QuestionWidget.newUniqueId());
        c.setText(prompt.getSelectChoiceText(mItems.get(i)));
        c.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
        c.setFocusable(!prompt.isReadOnly());
        c.setEnabled(!prompt.isReadOnly());

        for (int vi = 0; vi < ve.size(); vi++) {
          // match based on value, not key
          if (mItems.get(i).getValue().equals(ve.elementAt(vi).getValue())) {
            c.setChecked(true);
            break;
          }
        }
        mCheckboxes.add(c);
        // when clicked, check for readonly before toggling
        c.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!mCheckboxInit && mPrompt.isReadOnly()) {
                  if (buttonView.isChecked()) {
                    buttonView.setChecked(false);
                    Collect.getInstance()
                        .getActivityLogger()
                        .logInstanceAction(
                            this,
                            "onItemClick.deselect",
                            mItems.get((Integer) buttonView.getTag()).getValue(),
                            mPrompt.getIndex());
                  } else {
                    buttonView.setChecked(true);
                    Collect.getInstance()
                        .getActivityLogger()
                        .logInstanceAction(
                            this,
                            "onItemClick.select",
                            mItems.get((Integer) buttonView.getTag()).getValue(),
                            mPrompt.getIndex());
                  }
                }
              }
            });

        String audioURI = null;
        audioURI =
            prompt.getSpecialFormSelectChoiceText(mItems.get(i), FormEntryCaption.TEXT_FORM_AUDIO);

        String imageURI;
        if (mItems.get(i) instanceof ExternalSelectChoice) {
          imageURI = ((ExternalSelectChoice) mItems.get(i)).getImage();
        } else {
          imageURI =
              prompt.getSpecialFormSelectChoiceText(
                  mItems.get(i), FormEntryCaption.TEXT_FORM_IMAGE);
        }

        String videoURI = null;
        videoURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "video");

        String bigImageURI = null;
        bigImageURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "big-image");

        MediaLayout mediaLayout = new MediaLayout(getContext());
        mediaLayout.setAVT(
            prompt.getIndex(),
            "." + Integer.toString(i),
            c,
            audioURI,
            imageURI,
            videoURI,
            bigImageURI);
        addView(mediaLayout);

        // Last, add the dividing line between elements (except for the last element)
        ImageView divider = new ImageView(getContext());
        divider.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
        if (i != mItems.size() - 1) {
          addView(divider);
        }
      }
    }

    mCheckboxInit = false;
  }